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, ...
随机推荐
- FCC 成都社区·前端周刊 第 6 期
01. JS 引擎 V8 v6.6 的更新 最新 v6.6 版本的 V8 JavaScript 引擎更新了方法 Function.prototype.toString(),改进了代码缓存机制.异步性能 ...
- strtok的使用
/* strtok函数的使用 */ #include <stdio.h> #include <stdlib.h> #include <string.h> // 函数 ...
- 图片转base64的几种场景(网络图片,本地图片,用户上传图片)
转载于博客园 https://www.cnblogs.com/zhangdiIT/p/7895903.html 写的很棒 推荐给大家 场景一:将用户本地上传的资源转化,即用户通过浏览器点击文件上传时 ...
- HDU-6546-Function(贪心)
链接: https://vjudge.net/problem/HDU-6546 题意: wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n). 现在他想在 ...
- layui中从子窗口传递数据到父窗口,第三个子弹层的值传给第二个弹层
最近做一个项目的需要多个弹层,每个弹层中还需要数据传递, 经过测试,以下方法三个弹层才有效,如果只是有两个弹层,请用其它方法 大概如图,看图自己应该明白 如何在在b页面选择好的值传给a页面的问题,这个 ...
- Python 元组Ⅰ
Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: 创建空元 ...
- Composite UI Application Block(CAB)
序言 资料 https://www.cnblogs.com/lglruirui/archive/2010/06/21/1761737.html?tdsourcetag=s_pcqq_aiomsg ht ...
- docker安装xxl-job
一 安装mysql root@localhost tmp]# docker run -itd --name xxl-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD= ...
- cin.clear()、cin.sync()
看机器学习时,发现之前学的C++代码忘了,cin.clear().cin.sync() cin.clear():将流中的所有状态值都重设为有效值 cin.sync():清空流 这个很有意思,如果没有c ...
- Java连接Oracle数据库常用方法
JDBC的六大步骤: 注册驱动 获取连接 获取执行sql语句对象 执行sql语句 处理结果集 关闭资源 oracle URL: jdbc:oracle:thin:@localhost:1521:SID ...