BCD与字符串相互转换

ExpandedBlockStart.gif View Code
 1  bool  BCDtoStr( char   * str,unsigned  char   * BCD, int  BCD_length)
 2  {
 3       if (BCD == 0   ||  BCD_length == 0 )
 4           return   false ;
 5       int  i,j;
 6       for (i = 0 ,j = 0 ;i < BCD_length;i ++ ,j += 2 )
 7      {
 8          str[j] =  (BCD[i] >> 4 >   9   ?  (BCD[i] >> 4 ) - 10 + ' A '  : (BCD[i] >> 4 ) + ' 0 ' ;
 9          str[j + 1 ] = (BCD[i]  &   0x0F > 9   ?  (BCD[i]  &   0x0F ) - 10 + ' A '  : (BCD[i]  &   0x0F ) + ' 0 ' ;
10      }
11      str[j] = ' \0 ' ;
12       return   true ;
13  }
14  bool  StrtoBCD( char   * str,unsigned  char   * BCD, int   * BCD_length)
15  {
16       if (str == 0 return   false ;
17       int  tmp = strlen(str);
18      tmp -= tmp % 2 ;
19       if (tmp == 0 return   false ;
20       int  i,j;
21       for (i = 0 ;i < tmp;i ++ )
22      {
23           if (str == 0   ||
24               ! (str[i] >= ' 0 '   &&  str[i] <= ' 9 '   ||  str[i] >= ' a '   &&  str[i] <= ' f '   ||  str[i] >= ' A '   &&  str[i] <= ' F ' )
25              )
26               return   false ;
27      }
28      
29       for (i = 0 ,j = 0 ;i < tmp / 2 ;i ++ ,j += 2 )
30      {
31          str[j] > ' 9 '   ?  
32              (str[j] > ' F '   ?  BCD[i] = str[j] - ' a ' + 10  : BCD[i] = str[j] - ' A ' + 10 )
33              : BCD[i] = str[j] - ' 0 ' ;
34          str[j + 1 ] > ' 9 '   ?  
35              (str[j + 1 ] > ' F '   ?  BCD[i] = (BCD[i] << 4 ) + str[j + 1 ] - ' a ' + 10  : BCD[i] = (BCD[i] << 4 ) + str[j + 1 ] - ' A ' + 10 )
36              : BCD[i] = (BCD[i] << 4 ) + str[j + 1 ] - ' 0 ' ;        
37      }
38       if (BCD_length)
39           * BCD_length = tmp / 2 ;
40       return   true ;    
41  }

转载于:https://www.cnblogs.com/stli/archive/2011/04/15/2017640.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值