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. LOJ-6280-数列分块入门4

    链接: https://loj.ac/problem/6280 题意: 给出一个长为n 的数列,以及 n个操作,操作涉及区间加法,区间求和. 思路: sum维护区间和, tag维护每个区间多加的,不是 ...

  2. 深入理解vue 修饰符sync

    [ vue sync修饰符示例] 在说vue 修饰符sync前,我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移除了 ...

  3. 这里面ID为002和005的记录是重复的,在这里要把其中一条去掉,达到下面的效果:

    --去掉重复的记录 select ID,Code,ColorNum from (     SELECT      ROW_NUMBER() OVER(         PARTITION BY Cod ...

  4. 在postman中请求的接口有csrf怎么办

    今天在写项目的时候,写了一个post接口,为了防止crsf攻击,config.defalut.js文件中加了如下代码: exports.security = { csrf: { ignoreJSON: ...

  5. const与#define的区别

    1.const (1)为什么需要const成员函数? C中常用:“ #define 变量名 变量值”定义一个值替代,然而却有个致命缺点:缺乏类型检测机制,这样预处理在C++中成为可能引发错误的隐患,于 ...

  6. 2019hdu多校 Keen On Everything But Triangle

    Problem Description N sticks are arranged in a row, and their lengths are a1,a2,...,aN. There are Q ...

  7. 我的docker笔记

    下面的链接全部是我在CSDN的关于docker的博文,我认为已经很是详细了,没有再次总结的必要性,特给出链接地址 docker容器技术基础 https://blog.csdn.net/zisefeiz ...

  8. springboot(五).如何在springboot项目中使用拦截器

    在每个项目中,拦截器都是我们经常会去使用的东西,基本上任一一个项目都缺不了拦截器的使用. 如日志记录.登录验证,session验证等,都需要拦截器来拦截URL请求,那springboot中的拦截器是如 ...

  9. Luogu P5468 [NOI2019]回家路线 (斜率优化、DP)

    题目链接: (luogu) https://www.luogu.org/problemnew/show/P5468 题解: 爆long long毁一生 我太菜了,这题这么简单考场上居然没想到正解-- ...

  10. Updatexml函数再mysql中的作用

    函数的解释 http://www.blogjava.net/chenpengyi/archive/2006/07/11/57578.html 我的理解就是updatexml函数具有查询功能 并且会再x ...