zoukankan      html  css  js  c++  java
  • BCD与ASCII码互转-C语言实现

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /*BCD 与 ASCII码转换*/  
    2.   
    3. /******************************************************************* 
    4. 函数名:  asc2bcd  
    5. 功能描述:将ascii码转换为bcd码 
    6. 参数:     
    7.          bcd:转换后的BCD码 
    8.          asc:需转换的ASCII码串 
    9.          len:需转换的ascii码串长度 
    10.  
    11. 返回值:  uint32  
    12.                               0:成功 
    13.                               其他:失败 
    14. ********************************************************************/  
    15. uint32 asc2bcd(uint8* bcd, const uint8* asc, uint32 len);  
    16.   
    17. /******************************************************************* 
    18. 函数名: bcd2asc  
    19. 功能描述:将bcd码转换为ascii码串 
    20. 参数:     
    21.          asc:转换的ASCII码串 
    22.          bcd:需转换的BCD码 
    23.          len:需转换的BCD码长度 
    24.  
    25. 返回值:  uint32  
    26.                               0:成功 
    27.                               其他:失败 
    28. ********************************************************************/  
    29. uint32 bcd2asc(uint8* asc, const uint8* bcd, uint32 len);  


        

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #include <assert.h>  
    2.   
    3. #include "utils.h"  
    4. //基于查表实现BCD与Ascii之间的转换  
    5. static uint8 bcd2ascii[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};  
    6.   
    7. static uint8 ascii2bcd1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};  
    8. static uint8 ascii2bcd2[6]  = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};  
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. uint32   
    2. ASC2BCD(uint8 *bcd, const uint8 *asc, uint32 len)  
    3. {  
    4.     uint8 c = 0;  
    5.     uint8 index = 0;  
    6.     uint8 i = 0;    
    7.       
    8.     len >>= 1;      
    9.   
    10.     for(; i < len; i++) {  
    11.         //first BCD  
    12.         if(*asc >= 'A' && *asc <= 'F') {  
    13.             index = *asc - 'A';   
    14.             c  = ascii2bcd2[index] << 4;  
    15.         } else if(*asc >= '0' && *asc <= '9') {  
    16.             index = *asc - '0';  
    17.             c  = ascii2bcd1[index] << 4;  
    18.         }  
    19.         asc++;  
    20.   
    21.         //second BCD  
    22.         if(*asc >= 'A' && *asc <= 'F') {  
    23.             index = *asc - 'A';   
    24.             c  |= ascii2bcd2[index];  
    25.         } else if(*asc >= '0' && *asc <= '9') {  
    26.             index = *asc - '0';  
    27.             c  |= ascii2bcd1[index];  
    28.         }  
    29.         asc++;  
    30.   
    31.         *bcd++ = c;  
    32.     }     
    33.   
    34.     return 0;  
    35. }  
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. uint32  
    2. BCD2ASC (uint8 *asc, const uint8 *bcd, uint32 len)  
    3. {  
    4.     uint8 c = 0;  
    5.     uint8 i;  
    6.   
    7.   
    8.     for(i = 0; i < len; i++) {  
    9.         //first BCD  
    10.         c = *bcd >> 4;  
    11.         *asc++ = bcd2ascii[c];  
    12.   
    13.   
    14.         //second  
    15.         c = *bcd & 0x0f;  
    16.         *asc++ = bcd2ascii[c];  
    17.         bcd++;  
    18.     }  
    19.   
    20.   
    21.     return 0;  
    22. }  
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      1. int main(void)//测试程序  
      2. {  
      3.     const unsigned char ascii[12] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'};  
      4.     unsigned char bcd[6];  
      5.   
      6.     ASC2BCD(bcd, ascii, 12, 0);   
      7.       
      8.     int i = 0;  
      9.   
      10.     printf("ascii = %s ", ascii);  
      11.   
      12.     for(; i < 6; i++) {  
      13.         printf("bcd = 0x%.2x ", bcd[i]);  
      14.     }     
      15.   
      16.     /*   
      17.        unsigned char ascii[13] = {0}; 
      18.        const unsigned char bcd[6] = {0x01, 0x23, 0x45, 0xCD, 0xEF, 0xAB}; 
      19.  
      20.        BCD2ASC(ascii, bcd, 6, 0); 
      21.        printf("ascii = %s ", ascii); 
      22.        */  
      23.     return 0;  
      24. }  
  • 相关阅读:
    mongo常用查询
    MongoDB
    python连接mongo
    linux 硬盘
    kali 日志
    linux 命令
    grep
    linux shell
    linux 匹配字符串是否为数字
    linux 第一题 计算题
  • 原文地址:https://www.cnblogs.com/daochong/p/6530940.html
Copyright © 2011-2022 走看看