1. /*BCD 与 ASCII码转换*/
  2. /*******************************************************************
  3. 函数名:  asc2bcd
  4. 功能描述:将ascii码转换为bcd码
  5. 参数:
  6. bcd:转换后的BCD码
  7. asc:需转换的ASCII码串
  8. len:需转换的ascii码串长度
  9. 返回值:  uint32
  10. 0:成功
  11. 其他:失败
  12. ********************************************************************/
  13. uint32 asc2bcd(uint8* bcd, const uint8* asc, uint32 len);
  14. /*******************************************************************
  15. 函数名: bcd2asc
  16. 功能描述:将bcd码转换为ascii码串
  17. 参数:
  18. asc:转换的ASCII码串
  19. bcd:需转换的BCD码
  20. len:需转换的BCD码长度
  21. 返回值:  uint32
  22. 0:成功
  23. 其他:失败
  24. ********************************************************************/
  25. uint32 bcd2asc(uint8* asc, const uint8* bcd, uint32 len);
  1. #include <assert.h>
  2. #include "utils.h"
  3. //基于查表实现BCD与Ascii之间的转换
  4. static uint8 bcd2ascii[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  5. static uint8 ascii2bcd1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  6. static uint8 ascii2bcd2[6]  = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  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. len >>= 1;
  8. for(; i < len; i++) {
  9. //first BCD
  10. if(*asc >= 'A' && *asc <= 'F') {
  11. index = *asc - 'A';
  12. c  = ascii2bcd2[index] << 4;
  13. } else if(*asc >= '0' && *asc <= '9') {
  14. index = *asc - '0';
  15. c  = ascii2bcd1[index] << 4;
  16. }
  17. asc++;
  18. //second BCD
  19. if(*asc >= 'A' && *asc <= 'F') {
  20. index = *asc - 'A';
  21. c  |= ascii2bcd2[index];
  22. } else if(*asc >= '0' && *asc <= '9') {
  23. index = *asc - '0';
  24. c  |= ascii2bcd1[index];
  25. }
  26. asc++;
  27. *bcd++ = c;
  28. }
  29. return 0;
  30. }
  1. uint32
  2. BCD2ASC (uint8 *asc, const uint8 *bcd, uint32 len)
  3. {
  4. uint8 c = 0;
  5. uint8 i;
  6. for(i = 0; i < len; i++) {
  7. //first BCD
  8. c = *bcd >> 4;
  9. *asc++ = bcd2ascii[c];
  10. //second
  11. c = *bcd & 0x0f;
  12. *asc++ = bcd2ascii[c];
  13. bcd++;
  14. }
  15. return 0;
  16. }
    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. ASC2BCD(bcd, ascii, 12, 0);
    6. int i = 0;
    7. printf("ascii = %s\n", ascii);
    8. for(; i < 6; i++) {
    9. printf("bcd = 0x%.2x\n", bcd[i]);
    10. }
    11. /*
    12. unsigned char ascii[13] = {0};
    13. const unsigned char bcd[6] = {0x01, 0x23, 0x45, 0xCD, 0xEF, 0xAB};
    14. BCD2ASC(ascii, bcd, 6, 0);
    15. printf("ascii = %s\n", ascii);
    16. */
    17. return 0;
    18. }

BCD与ASCII码互转-C语言实现的更多相关文章

  1. [PHP] chr和ord函数实现字符串和ASCII码互转

    chr和ord函数是用来字符串和ASCII码互转的.  ASCII码是计算机所能显示字符的编码,它的取值范围是0-255,其中包括标点.字母.数字.汉字等.在编程过程中,经常把指定的字符转化为ASCI ...

  2. 用php的chr和ord函数实现字符串和ASCII码互转

    http://shenyongqang.blog.163.com/blog/static/22439113201002941856838/ chr和ord函数是用来字符串和ASCII码互转的. ASC ...

  3. 将一个字符与对应Ascii码互转

    package nicetime.com.practies; /** * Java中将一个字符与对应Ascii码互转 1 byte = 8bit 可以表示 0-127 */public class G ...

  4. java 字符与ASCII码互转

    字符转对应ASCII码 // 方法一:将char强制转换为byte char ch = 'A'; byte byteAscii = (byte) ch; System.out.println(byte ...

  5. js 字符与ASCII码互转

    将字符转为ASCII码 var str = "A"; str.charCodeAt(); var str1 = 'a'; str1.charCodeAt(); 将ASCII码转为字 ...

  6. Java字符串跟ASCII码互转

    1.由于项目中遇到,在服务器端起的jar包程序,给前台发消息后,前段收到的消息出现乱码情况,所以采取在后才发消息前先把消息字符串转成ASCII码再发往前台,前台采取在收到后台消息先把ASCII码转成字 ...

  7. js字符与ASCII码互转的方法

    大写字母A-Z对应的ASCII码值是65-90 小写字母a-z对应的ASCII码值是97-122 将字母转为ascii码的方法: 将ascii码转为对应字母的方法:

  8. C语言 16进制与ascii码互转

    /*把ASCII字符转换为16进制 */ uint8_t char_to_hex(const uint8_t *ch) { uint8_t value = 0; if(*ch >= 0 & ...

  9. 排坑&#183;ASCII码为160的空格(nbsp)

    阅文时长 | 2.83分钟 字数统计 | 1345.2字符 『排坑·ASCII码为160的空格(nbsp)』 编写人 | SCscHero 编写时间 | Wednesday, September 9, ...

随机推荐

  1. FCC 成都社区·前端周刊 第 6 期

    01. JS 引擎 V8 v6.6 的更新 最新 v6.6 版本的 V8 JavaScript 引擎更新了方法 Function.prototype.toString(),改进了代码缓存机制.异步性能 ...

  2. strtok的使用

    /* strtok函数的使用 */ #include <stdio.h> #include <stdlib.h> #include <string.h> // 函数 ...

  3. 图片转base64的几种场景(网络图片,本地图片,用户上传图片)

    转载于博客园 https://www.cnblogs.com/zhangdiIT/p/7895903.html 写的很棒  推荐给大家 场景一:将用户本地上传的资源转化,即用户通过浏览器点击文件上传时 ...

  4. HDU-6546-Function(贪心)

    链接: https://vjudge.net/problem/HDU-6546 题意: wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n). 现在他想在 ...

  5. layui中从子窗口传递数据到父窗口,第三个子弹层的值传给第二个弹层

    最近做一个项目的需要多个弹层,每个弹层中还需要数据传递, 经过测试,以下方法三个弹层才有效,如果只是有两个弹层,请用其它方法 大概如图,看图自己应该明白 如何在在b页面选择好的值传给a页面的问题,这个 ...

  6. Python 元组Ⅰ

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: 创建空元 ...

  7. Composite UI Application Block(CAB)

    序言 资料 https://www.cnblogs.com/lglruirui/archive/2010/06/21/1761737.html?tdsourcetag=s_pcqq_aiomsg ht ...

  8. docker安装xxl-job

    一 安装mysql root@localhost tmp]# docker run -itd --name xxl-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD= ...

  9. cin.clear()、cin.sync()

    看机器学习时,发现之前学的C++代码忘了,cin.clear().cin.sync() cin.clear():将流中的所有状态值都重设为有效值 cin.sync():清空流 这个很有意思,如果没有c ...

  10. Java连接Oracle数据库常用方法

    JDBC的六大步骤: 注册驱动 获取连接 获取执行sql语句对象 执行sql语句 处理结果集 关闭资源 oracle URL: jdbc:oracle:thin:@localhost:1521:SID ...