CAPL脚本中常用到的数据类型转换—— BCD 码转为 Byte数组
目录BCD 码转为 Byte数组BCD 码转为 字符串BCD 码转为 Byte数组源程序:byte GBF_ConvertBCDToByteArr(byte bcdRawData[], word numOfBCDs, byte outBCDArr[]){word i;word byteIndex;long tmpVal;byte retVal;char tmpStr[10];char tmpErr
·
BCD 码转为 Byte数组
- 源程序:
byte GBF_ConvertBCDToByteArr(byte bcdRawData[], word numOfBCDs, byte outBCDArr[])
{
word i;
word byteIndex;
long tmpVal;
byte retVal;
char tmpStr[10];
char tmpErrStr[gcText200];
retVal = gcNok;
// VS/dsa: Reset output array
for (i = 0; i < elcount(outBCDArr); i++)
{
outBCDArr[i] = 0;
}
//VS/dsa: Check that the supplied data can be used to create the specified number of BCDs
if ((elcount(bcdRawData) * 2) < numOfBCDs)
{
snprintf(tmpErrStr, elcount (tmpErrStr), "GBF_ConvertBCDToByteArr: ERROR: Input parameters do not match, numOfBCDs is %d but bcdRawData only contains %d elements!", numOfBCDs, elcount(bcdRawData));
write(tmpErrStr);
}
else
{
// VS/dsa: Also check that the supplied output array can hold all BCD values
if(elcount(outBCDArr) < (numOfBCDs))
{
write ("GBF_ConvertBCDToByteArr: ERROR: Input parameters do not match, numOfBCDs is %d but outBCDArr only contains %d elements!", numOfBCDs, elcount(outBCDArr));
}
else
{
//VS/dsa: All checks went fine, convert data
for (i = 0; i < numOfBCDs; i++)
{
byteIndex = i / 2;
if (0 == (i % 2))
{
outBCDArr[i] = (byte)(bcdRawData[byteIndex] >> 4);
}
else
{
outBCDArr[i] = (byte)(bcdRawData[byteIndex] & 0x0F);
}
}
retVal = gcOk;
}
}
return retVal;
}
- 测试代码
{
byte in_byte_array[4]={0x12,0x34,0x56,0x78};
byte out_byte_array[9];
GBF_ConvertBCDToByteArr(in_byte_array,4*2,out_byte_array);
write("out_byte_array[9]={0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x}; ",out_byte_array[0],out_byte_array[1],out_byte_array[2],out_byte_array[3],out_byte_array[4],out_byte_array[5],out_byte_array[6],out_byte_array[7],out_byte_array[8]);
}
- 测试结果:
out_byte_array[9]={0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x0};
BCD 码转为 字符串
- 源程序:
byte GBF_ConvertBCDToStr(byte bcdRawData[], word numOfBCDs, char outBCDStr[])
{
word i;
word byteIndex;
long tmpVal;
byte retVal;
char tmpStr[10];
char tmpErrStr[gcText200];
// VS/dsa: Init to failed
retVal = gcNok;
// VS/dsa: Reset output string
strncpy(outBCDStr, "", elcount(outBCDStr));
//VS/dsa: Check that the supplied data can be used to create the specified number of BCDs
if ((elcount(bcdRawData) * 2) < numOfBCDs)
{
snprintf(tmpErrStr, elcount (tmpErrStr), "GBF_ConvertBCDToStr: ERROR: Input parameters do not match, numOfBCDs is %d but bcdRawData only contains %d elements!", numOfBCDs, elcount(bcdRawData));
write(tmpErrStr);
}
else
{
// VS/dsa: Also check that the supplied output array can hold all BCD values and a string terminator character
if(elcount(outBCDStr) < (numOfBCDs + 1))
{
snprintf(tmpErrStr, elcount (tmpErrStr), "GBF_ConvertBCDToStr: ERROR: Input parameters do not match, numOfBCDs is %d but outBCDStr only contains %d elements!", numOfBCDs, elcount(outBCDStr));
write(tmpErrStr);
}
else
{
//VS/dsa: All checks went fine, convert data
for (i = 0; i < numOfBCDs; i++)
{
// VS/dsa: The byte index to use for accessing bcdRawData is e.g. 0 if i == 0 or 1 and 1 if i == 3 or 4
byteIndex = i / 2;
// VS/dsa: Use the modulo operator % to check if this is an even or odd iteration
// For even iterations just shift the most significant four bits four positions to the right
// For odd iterations filter out the least significant four bits, no shift needed
if (0 == (i % 2))
{
tmpVal = (long)(bcdRawData[byteIndex] >> 4);
}
else
{
tmpVal = (long)(bcdRawData[byteIndex] & 0x0F);
}
// VS/dsa: Convert value to a temp string. Always use base 10 for conversion since this is BCD.
ltoa(tmpVal, tmpStr, 10);
// VS/dsa: Build output string. Note: Since each value is appended at the end of the string, we must start the iteration
// with the most significant value
strncat(outBCDStr, tmpStr, elcount(outBCDStr));
}
retVal = gcOk;
}
}
return retVal;
}
- 测试代码:
{
byte in_byte_array[4]={0x12,0x34,0x56,0x78};
char out_char_array[9];
GBF_ConvertBCDToStr(in_byte_array,8,out_char_array);
write("out_char_array[9]=%s; ",out_char_array);
}
- 测试结果:
out_char_array[9]=12345678;
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)