BCD与ASCII码互转-C语言实现
- /*BCD 与 ASCII码转换*/
- /*******************************************************************
- 函数名: asc2bcd
- 功能描述:将ascii码转换为bcd码
- 参数:
- bcd:转换后的BCD码
- asc:需转换的ASCII码串
- len:需转换的ascii码串长度
- 返回值: uint32
- 0:成功
- 其他:失败
- ********************************************************************/
- uint32 asc2bcd(uint8* bcd, const uint8* asc, uint32 len);
- /*******************************************************************
- 函数名: bcd2asc
- 功能描述:将bcd码转换为ascii码串
- 参数:
- asc:转换的ASCII码串
- bcd:需转换的BCD码
- len:需转换的BCD码长度
- 返回值: uint32
- 0:成功
- 其他:失败
- ********************************************************************/
- uint32 bcd2asc(uint8* asc, const uint8* bcd, uint32 len);
- #include <assert.h>
- #include "utils.h"
- //基于查表实现BCD与Ascii之间的转换
- static uint8 bcd2ascii[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- static uint8 ascii2bcd1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
- static uint8 ascii2bcd2[6] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
- uint32
- ASC2BCD(uint8 *bcd, const uint8 *asc, uint32 len)
- {
- uint8 c = 0;
- uint8 index = 0;
- uint8 i = 0;
- len >>= 1;
- for(; i < len; i++) {
- //first BCD
- if(*asc >= 'A' && *asc <= 'F') {
- index = *asc - 'A';
- c = ascii2bcd2[index] << 4;
- } else if(*asc >= '0' && *asc <= '9') {
- index = *asc - '0';
- c = ascii2bcd1[index] << 4;
- }
- asc++;
- //second BCD
- if(*asc >= 'A' && *asc <= 'F') {
- index = *asc - 'A';
- c |= ascii2bcd2[index];
- } else if(*asc >= '0' && *asc <= '9') {
- index = *asc - '0';
- c |= ascii2bcd1[index];
- }
- asc++;
- *bcd++ = c;
- }
- return 0;
- }
- uint32
- BCD2ASC (uint8 *asc, const uint8 *bcd, uint32 len)
- {
- uint8 c = 0;
- uint8 i;
- for(i = 0; i < len; i++) {
- //first BCD
- c = *bcd >> 4;
- *asc++ = bcd2ascii[c];
- //second
- c = *bcd & 0x0f;
- *asc++ = bcd2ascii[c];
- bcd++;
- }
- return 0;
- }
- int main(void)//测试程序
- {
- const unsigned char ascii[12] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C'};
- unsigned char bcd[6];
- ASC2BCD(bcd, ascii, 12, 0);
- int i = 0;
- printf("ascii = %s\n", ascii);
- for(; i < 6; i++) {
- printf("bcd = 0x%.2x\n", bcd[i]);
- }
- /*
- unsigned char ascii[13] = {0};
- const unsigned char bcd[6] = {0x01, 0x23, 0x45, 0xCD, 0xEF, 0xAB};
- BCD2ASC(ascii, bcd, 6, 0);
- printf("ascii = %s\n", ascii);
- */
- return 0;
- }
BCD与ASCII码互转-C语言实现的更多相关文章
- [PHP] chr和ord函数实现字符串和ASCII码互转
chr和ord函数是用来字符串和ASCII码互转的. ASCII码是计算机所能显示字符的编码,它的取值范围是0-255,其中包括标点.字母.数字.汉字等.在编程过程中,经常把指定的字符转化为ASCI ...
- 用php的chr和ord函数实现字符串和ASCII码互转
http://shenyongqang.blog.163.com/blog/static/22439113201002941856838/ chr和ord函数是用来字符串和ASCII码互转的. ASC ...
- 将一个字符与对应Ascii码互转
package nicetime.com.practies; /** * Java中将一个字符与对应Ascii码互转 1 byte = 8bit 可以表示 0-127 */public class G ...
- java 字符与ASCII码互转
字符转对应ASCII码 // 方法一:将char强制转换为byte char ch = 'A'; byte byteAscii = (byte) ch; System.out.println(byte ...
- js 字符与ASCII码互转
将字符转为ASCII码 var str = "A"; str.charCodeAt(); var str1 = 'a'; str1.charCodeAt(); 将ASCII码转为字 ...
- Java字符串跟ASCII码互转
1.由于项目中遇到,在服务器端起的jar包程序,给前台发消息后,前段收到的消息出现乱码情况,所以采取在后才发消息前先把消息字符串转成ASCII码再发往前台,前台采取在收到后台消息先把ASCII码转成字 ...
- js字符与ASCII码互转的方法
大写字母A-Z对应的ASCII码值是65-90 小写字母a-z对应的ASCII码值是97-122 将字母转为ascii码的方法: 将ascii码转为对应字母的方法:
- C语言 16进制与ascii码互转
/*把ASCII字符转换为16进制 */ uint8_t char_to_hex(const uint8_t *ch) { uint8_t value = 0; if(*ch >= 0 & ...
- 排坑·ASCII码为160的空格(nbsp)
阅文时长 | 2.83分钟 字数统计 | 1345.2字符 『排坑·ASCII码为160的空格(nbsp)』 编写人 | SCscHero 编写时间 | Wednesday, September 9, ...
随机推荐
- LOJ-6280-数列分块入门4
链接: https://loj.ac/problem/6280 题意: 给出一个长为n 的数列,以及 n个操作,操作涉及区间加法,区间求和. 思路: sum维护区间和, tag维护每个区间多加的,不是 ...
- 深入理解vue 修饰符sync
[ vue sync修饰符示例] 在说vue 修饰符sync前,我们先看下官方文档:vue .sync 修饰符,里面说vue .sync 修饰符以前存在于vue1.0版本里,但是在在 2.0 中移除了 ...
- 这里面ID为002和005的记录是重复的,在这里要把其中一条去掉,达到下面的效果:
--去掉重复的记录 select ID,Code,ColorNum from ( SELECT ROW_NUMBER() OVER( PARTITION BY Cod ...
- 在postman中请求的接口有csrf怎么办
今天在写项目的时候,写了一个post接口,为了防止crsf攻击,config.defalut.js文件中加了如下代码: exports.security = { csrf: { ignoreJSON: ...
- const与#define的区别
1.const (1)为什么需要const成员函数? C中常用:“ #define 变量名 变量值”定义一个值替代,然而却有个致命缺点:缺乏类型检测机制,这样预处理在C++中成为可能引发错误的隐患,于 ...
- 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 ...
- 我的docker笔记
下面的链接全部是我在CSDN的关于docker的博文,我认为已经很是详细了,没有再次总结的必要性,特给出链接地址 docker容器技术基础 https://blog.csdn.net/zisefeiz ...
- springboot(五).如何在springboot项目中使用拦截器
在每个项目中,拦截器都是我们经常会去使用的东西,基本上任一一个项目都缺不了拦截器的使用. 如日志记录.登录验证,session验证等,都需要拦截器来拦截URL请求,那springboot中的拦截器是如 ...
- Luogu P5468 [NOI2019]回家路线 (斜率优化、DP)
题目链接: (luogu) https://www.luogu.org/problemnew/show/P5468 题解: 爆long long毁一生 我太菜了,这题这么简单考场上居然没想到正解-- ...
- Updatexml函数再mysql中的作用
函数的解释 http://www.blogjava.net/chenpengyi/archive/2006/07/11/57578.html 我的理解就是updatexml函数具有查询功能 并且会再x ...