CAPL脚本中常用到的数据类型转换—— BCD 码转为 Byte数组

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; 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蚂蚁小兵

慢慢长夜磨一章好文章,费烟!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值