16进制和ASCII的互相转换
ztj100 2024-11-24 01:32 31 浏览 0 评论
/*
16进制和ASCII的互相转换
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
typedef double flo64; // Double precision floating point
typedef double * pflo64;
typedef float flo32; // Single precision floating point
typedef float * pflo32;
typedef signed int int32s; // Signed 32 bit quantity
typedef signed int * pint32s;
typedef unsigned int int32u; // Unsigned 32 bit quantity
typedef unsigned int * pint32u;
typedef signed short int16s; // Signed 16 bit quantity
typedef signed short* pint16s;
typedef unsigned short int16u; // Unsigned 16 bit quantity
typedef unsigned short* pint16u;
typedef signed char int8s; // Signed 8 bit quantity
typedef signed char * pint8s;
typedef unsigned char int8u; // Unsigned 8 bit quantity
typedef unsigned char * pint8u;
#define MAX_FRAM_LEN 256
unsigned char halfbyte_2ascii_biggercase(unsigned char half_byte)
{
//printf("1half_byte:%02x\n", half_byte);
//printf("1half_byte:%02x, %c\n", half_byte + '0', half_byte + '0');
//大写形式
if(half_byte >= 9) //0xa的十进制是10.字符'A'的16进制为0x41, 字符'a'的16进制为0x61,
half_byte += 39;
//printf("2half_byte:%02x\n", half_byte);
//printf("2half_byte + '0':%02x,%c\n", half_byte + '0', half_byte + '0');
return half_byte + '0';
}
unsigned char halfbyte_2ascii_lowercase(unsigned char half_byte)
{
//printf("1half_byte:%02x\n", half_byte);
//printf("1half_byte:%02x, %c\n", half_byte + '0', half_byte + '0');
#if 1
//大写形式
if(half_byte >= 9) //0xa的十进制是10.字符'A'的16进制为0x41, 字符'a'的16进制为0x61,
half_byte += 7;
#endif
//printf("2half_byte:%02x\n", half_byte);
//printf("2half_byte + '0':%02x,%c\n", half_byte + '0', half_byte + '0');
return half_byte + '0';
}
/*
16进制转16进制ASCII(大写)
hex 30 30 30 33 30 30 32 30 30 30 30 32 3d 3b ==>ascii hex: 30 30 30 33 30 30 32 30 30 30 30 32 64 62
示例:unsigned char ascii1[30]={0};
unsigned char ascii2[30]={0};
int ascii1_len = 7;
int ascii2_len = 7;
memcpy(ascii1, "\x00\x03\x00\x20\x00\x02\xdb", ascii1_len);
memcpy(ascii2, "\x00\x03\x00\x20\x00\x02\xdb", ascii2_len);
hex2ascii_lowercase(ascii1, &ascii1_len);
printf("ascii1_len:%d,%02x\n", ascii1_len, ascii1_len);
for(int i = 0; i< ascii1_len; i++)
{
printf("%02x ", ascii1[i]);
}
printf("\n");
hex2ascii_biggercase(ascii2, &ascii2_len);
printf("ascii2_len:%d,%02x\n", ascii2_len, ascii2_len);
for(int i = 0; i< ascii2_len; i++)
{
printf("%02x ", ascii2[i]);
}
printf("\n");
结果:
ascii1_len:14,0e
30 30 30 33 30 30 32 30 30 30 30 32 44 42
ascii2_len:14,0e
30 30 30 33 30 30 32 30 30 30 30 32 64 62
*/
unsigned char hex2ascii_biggercase(unsigned char *pbuf,unsigned int *count)
{
unsigned char temp1,temp2,*pdata;
unsigned int i = 0,j = 0,sum;
if(pbuf == 0 || count == 0 || *count == 0) return 1;
if((pdata = (unsigned char *)malloc((*count+1)*sizeof(unsigned char))) == NULL) return 2; //内存申请,目前采用堆空间,长度512字节
sum = (*count);
/*每个字节用2个ASCII表示*/
for(i = 0; i < sum; i++)
{
temp1 = ((pbuf[i] & 0xF0) >> 4);
temp2 = (pbuf[i] & 0x0F);
pdata[j++] = halfbyte_2ascii_biggercase(temp1);
pdata[j++] = halfbyte_2ascii_biggercase(temp2);
}
*count = j; //转译后的总长度
memcpy(&pbuf[0],pdata,j); //转译后内容拷贝
/*释放内存*/
free(pdata);
pdata = NULL;
return 0;
}
/*
16进制转16进制ASCII(小写)
hex 3a 00 03 00 20 00 02 db 0d 0a ==>ascii hex: 30 30 30 33 30 30 32 30 30 30 30 32 44 42
示例:
unsigned char ascii1[30]={0};
unsigned char ascii2[30]={0};
int ascii1_len = 7;
int ascii2_len = 7;
memcpy(ascii1, "\x00\x03\x00\x20\x00\x02\xdb", ascii1_len);
memcpy(ascii2, "\x00\x03\x00\x20\x00\x02\xdb", ascii2_len);
hex2ascii_lowercase(ascii1, &ascii1_len);
printf("ascii1_len:%d,%02x\n", ascii1_len, ascii1_len);
for(int i = 0; i< ascii1_len; i++)
{
printf("%02x ", ascii1[i]);
}
printf("\n");
hex2ascii_biggercase(ascii2, &ascii2_len);
printf("ascii2_len:%d,%02x\n", ascii2_len, ascii2_len);
for(int i = 0; i< ascii2_len; i++)
{
printf("%02x ", ascii2[i]);
}
printf("\n");
结果:
ascii1_len:14,0e
30 30 30 33 30 30 32 30 30 30 30 32 44 42
ascii2_len:14,0e
30 30 30 33 30 30 32 30 30 30 30 32 64 62
*/
int8u hex2ascii_lowercase(int8u *pbuf,unsigned int *count)
{
int8u temp1,temp2,*pdata;
unsigned int i = 0,j = 0,sum;
if(pbuf == 0 || count == 0 || *count == 0) return 1;
if((pdata = (int8u *)malloc((*count+1)*sizeof(int8u))) == NULL) return 2; //内存申请,目前采用堆空间,长度512字节
sum = (*count);
/*每个字节用2个ASCII表示*/
for(i = 0; i < sum; i++)
{
temp1 = ((pbuf[i] & 0xF0) >> 4);
temp2 = (pbuf[i] & 0x0F);
pdata[j++] = halfbyte_2ascii_lowercase(temp1);
pdata[j++] = halfbyte_2ascii_lowercase(temp2);
}
*count = j; //转译后的总长度
memcpy(&pbuf[0],pdata,j); //转译后内容拷贝
/*释放内存*/
free(pdata);
pdata = NULL;
return 0;
}
/*
计算lrc校验,自研485温湿度用。
src:源字符串
start:起始下标
len:参数校验的有效数据长度
lrc:结果
示例:
unsigned char lsrc = 0;
unsigned char src[]="\x3A\x00\x03\x00\x20\x00\x02\xDB\x0D\x0A";
lsrc = count_lrc(src, 1, 6);
printf("lsrc:%d,%02x\n", lsrc, lsrc);
*/
unsigned char count_lrc(unsigned char *src, int start, int len)
{
unsigned char sum = 0;
unsigned char result = 0;
for(int i = start; i<= len; i++)
{
//printf("src[%d]:%d,%02x\n", i, src[i], src[i]);
sum+=src[i];
}
result = ~sum+1;
//printf("sum:%d,%02x\n", sum, sum);
//printf("result:%d,%02x\n", result, result);
return result;
}
/*
把modbus ascii数据转换成16进制(不改变原始数据)
pbuf:原始数据 16进制显示:3A 30 30 30 33 30 30 32 30 30 30 30 32 64 62 0D 0A ,对应字符串:000300200002db
src_len:原始数据长
dest:目标 16进制显示:3a 00 03 00 20 00 02 db 0d 0a
dest_len:目标长
例:
unsigned char p[]="\x3A\x30\x30\x30\x33\x30\x30\x32\x30\x30\x30\x30\x32\x64\x62\x0D\x0A";
int src_len = 17;
int dest_len = 0;
convert_modbus_ascii_2hex(p, src_len, dest, &dest_len);
printf("dest_len:%d,%02x\n", dest_len, dest_len);
for(int i = 0; i< dest_len; i++)
{
printf("%02x ", dest[i]);
}
printf("\n");
结果:
dest_len:10,0a
3a 00 03 00 20 00 02 db 0d 0a
*/
unsigned char convert_modbus_ascii_2hex(unsigned char *pbuf,int src_len, unsigned char *dest, int *dest_len)
{
int len_befor_after = 2;
unsigned char *pdata,temp1,temp2;
int32u i=0,j=0,sum=0;
if(pbuf==0 || src_len==0) return 1;
if((pdata = (unsigned char *)malloc((src_len+1)*sizeof(unsigned char))) == NULL) return 2; //内存申请,目前采用堆空间
memcpy(dest,pbuf,src_len);
sum = (src_len - len_befor_after); //帧头不参与
for(i = 1; i < sum; i+=2)
{
//printf("1pbuf[%d]:%02x\n", i, pbuf[i]);
switch(pbuf[i])
{
case 0x20: //代表空格
temp1 = 0x00;
break;
case 0x30:
temp1 = 0x00;
break;
case 0x31:
temp1 = 0x01;
break;
case 0x32:
temp1 = 0x02;
break;
case 0x33:
temp1 = 0x03;
break;
case 0x34:
temp1 = 0x04;
break;
case 0x35:
temp1 = 0x05;
break;
case 0x36:
temp1 = 0x06;
break;
case 0x37:
temp1 = 0x07;
break;
case 0x38:
temp1 = 0x08;
break;
case 0x39:
temp1 = 0x09;
break;
case 0x41:
temp1 = 0x0A;
break;
case 0x42:
temp1 = 0x0B;
break;
case 0x43:
temp1 = 0x0C;
break;
case 0x44:
temp1 = 0x0D;
break;
case 0x45:
temp1 = 0x0E;
break;
case 0x46:
temp1 = 0x0F;
break;
case 0x61:
temp1 = 0x0a;
break;
case 0x62:
temp1 = 0x0b;
break;
case 0x63:
temp1 = 0x0c;
break;
case 0x64:
temp1 = 0x0d;
break;
case 0x65:
temp1 = 0x0e;
break;
case 0x66:
temp1 = 0x0f;
break;
default:
temp1 = 0;
break;
}
//printf("2pbuf[%d]:%02x\n", i+1, pbuf[i+1]);
switch(pbuf[i+1])
{
case 0x20: //代表空格
temp2 = 0x00;
break;
case 0x30:
temp2 = 0x00;
break;
case 0x31:
temp2 = 0x01;
break;
case 0x32:
temp2 = 0x02;
break;
case 0x33:
temp2 = 0x03;
break;
case 0x34:
temp2 = 0x04;
break;
case 0x35:
temp2 = 0x05;
break;
case 0x36:
temp2 = 0x06;
break;
case 0x37:
temp2 = 0x07;
break;
case 0x38:
temp2 = 0x08;
break;
case 0x39:
temp2 = 0x09;
break;
case 0x41:
temp2 = 0x0A;
break;
case 0x42:
temp2 = 0x0B;
break;
case 0x43:
temp2 = 0x0C;
break;
case 0x44:
temp2 = 0x0D;
break;
case 0x45:
temp2 = 0x0E;
break;
case 0x46:
temp2 = 0x0F;
break;
case 0x61:
temp2 = 0x0a;
break;
case 0x62:
temp2 = 0x0b;
break;
case 0x63:
temp2 = 0x0c;
break;
case 0x64:
temp2 = 0x0d;
break;
case 0x65:
temp2 = 0x0e;
break;
case 0x66:
temp2 = 0x0f;
break;
default:
temp2 = 0;
break;
}
pdata[j++] = ((temp1 & 0x0F) << 4 | temp2); //2个ASCII合并为1个16进制
//printf("temp1:%02x\n", temp1);
//printf("temp2:%02x\n", temp2);
//printf("3pdata[%d]:%02x\n", j-1, pdata[j-1]);
}
#if 0
//计算校验
unsigned char lsrc = count_lrc(pdata, 1, 6);
pdata[j-1] = lsrc;
#endif
/*添加帧尾,构成原始帧数据*/
pdata[j++] = 0x0D;
pdata[j++] = 0x0A;
*dest_len = (j+1); //再添加帧头1个字节
/*帧头不参与处理*/
memcpy(&dest[1],pdata,j);
/*释放内存*/
free(pdata);
pdata = NULL;
return 0;
}
/*
把modbus ascii数据转换成16进制(改变原始数据和长度)
pbuf:原始数据 16进制显示:3A 30 30 30 33 30 30 32 30 30 30 30 32 64 62 0D 0A ,对应字符串:000300200002db 转换结果--》3a 00 03 00 20 00 02 db 0d 0a
src_len:原始数据长,结果会改变长度
例:
unsigned char p2[]="\x3A\x30\x30\x30\x33\x30\x30\x32\x30\x30\x30\x30\x32\x64\x62\x0D\x0A";
int len = 17;
convert_modbus_ascii_2hex_changesrc(p2, &len);
printf("len:%d,%02x\n", len, len);
for(int i = 0; i< len; i++)
{
printf("%02x ", p2[i]);
}
printf("\n");
结果:
dest_len:10,0a
3a 00 03 00 20 00 02 db 0d 0a
*/
unsigned char convert_modbus_ascii_2hex_changesrc(unsigned char *pbuf,int *src_len)
{
int len_befor_after = 2;
unsigned char *pdata,temp1,temp2;
int32u i=0,j=0,sum=0;
if(pbuf==0 || src_len==NULL || *src_len==0) return 1;
if((pdata = (unsigned char *)malloc((*src_len+1)*sizeof(unsigned char))) == NULL) return 2; //内存申请,目前采用堆空间
sum = (*src_len - len_befor_after); //帧头不参与
for(i = 1; i < sum; i+=2)
{
//printf("1pbuf[%d]:%02x\n", i, pbuf[i]);
switch(pbuf[i])
{
case 0x20: //代表空格
temp1 = 0x00;
break;
case 0x30:
temp1 = 0x00;
break;
case 0x31:
temp1 = 0x01;
break;
case 0x32:
temp1 = 0x02;
break;
case 0x33:
temp1 = 0x03;
break;
case 0x34:
temp1 = 0x04;
break;
case 0x35:
temp1 = 0x05;
break;
case 0x36:
temp1 = 0x06;
break;
case 0x37:
temp1 = 0x07;
break;
case 0x38:
temp1 = 0x08;
break;
case 0x39:
temp1 = 0x09;
break;
case 0x41:
temp1 = 0x0A;
break;
case 0x42:
temp1 = 0x0B;
break;
case 0x43:
temp1 = 0x0C;
break;
case 0x44:
temp1 = 0x0D;
break;
case 0x45:
temp1 = 0x0E;
break;
case 0x46:
temp1 = 0x0F;
break;
case 0x61:
temp1 = 0x0a;
break;
case 0x62:
temp1 = 0x0b;
break;
case 0x63:
temp1 = 0x0c;
break;
case 0x64:
temp1 = 0x0d;
break;
case 0x65:
temp1 = 0x0e;
break;
case 0x66:
temp1 = 0x0f;
break;
default:
temp1 = 0;
break;
}
//printf("2pbuf[%d]:%02x\n", i+1, pbuf[i+1]);
switch(pbuf[i+1])
{
case 0x20: //代表空格
temp2 = 0x00;
break;
case 0x30:
temp2 = 0x00;
break;
case 0x31:
temp2 = 0x01;
break;
case 0x32:
temp2 = 0x02;
break;
case 0x33:
temp2 = 0x03;
break;
case 0x34:
temp2 = 0x04;
break;
case 0x35:
temp2 = 0x05;
break;
case 0x36:
temp2 = 0x06;
break;
case 0x37:
temp2 = 0x07;
break;
case 0x38:
temp2 = 0x08;
break;
case 0x39:
temp2 = 0x09;
break;
case 0x41:
temp2 = 0x0A;
break;
case 0x42:
temp2 = 0x0B;
break;
case 0x43:
temp2 = 0x0C;
break;
case 0x44:
temp2 = 0x0D;
break;
case 0x45:
temp2 = 0x0E;
break;
case 0x46:
temp2 = 0x0F;
break;
case 0x61:
temp2 = 0x0a;
break;
case 0x62:
temp2 = 0x0b;
break;
case 0x63:
temp2 = 0x0c;
break;
case 0x64:
temp2 = 0x0d;
break;
case 0x65:
temp2 = 0x0e;
break;
case 0x66:
temp2 = 0x0f;
break;
default:
temp2 = 0;
break;
}
pdata[j++] = ((temp1 & 0x0F) << 4 | temp2); //2个ASCII合并为1个16进制
//printf("temp1:%02x\n", temp1);
//printf("temp2:%02x\n", temp2);
//printf("3pdata[%d]:%02x\n", j-1, pdata[j-1]);
}
#if 0
//计算校验
unsigned char lsrc = count_lrc(pdata, 1, 6);
pdata[j-1] = lsrc;
#endif
/*添加帧尾,构成原始帧数据*/
pdata[j++] = 0x0D;
pdata[j++] = 0x0A;
*src_len = (j+1); //再添加帧头1个字节
/*帧头不参与处理*/
memcpy(&pbuf[1],pdata,j);
/*释放内存*/
free(pdata);
pdata = NULL;
return 0;
}
int main(int argc, char *argv[])
{
//===1======16进制ASCII 转16进制测试===============//
unsigned char lsrc = 0;
unsigned char src[]="\x3A\x00\x03\x00\x20\x00\x02\xDB\x0D\x0A";
lsrc = count_lrc(src, 1, 6);
printf("lsrc:%d,%02x\n", lsrc, lsrc);
unsigned char dest[512] = {0};
//3A 30 30 30 33 30 30 32 30 30 30 30 32 64 62 0D 0A //:000300200002db
unsigned char p[]="\x3A\x30\x30\x30\x33\x30\x30\x32\x30\x30\x30\x30\x32\x64\x62\x0D\x0A";
int src_len = 17;
int dest_len = 0;
convert_modbus_ascii_2hex(p, src_len, dest, &dest_len);
printf("dest_len:%d,%02x\n", dest_len, dest_len);
for(int i = 0; i< dest_len; i++)
{
printf("%02x ", dest[i]);
}
printf("\n");
unsigned char p2[]="\x3A\x30\x30\x30\x33\x30\x30\x32\x30\x30\x30\x30\x32\x64\x62\x0D\x0A";
int len = 17;
convert_modbus_ascii_2hex_changesrc(p2, &len);
printf("len:%d,%02x\n", len, len);
for(int i = 0; i< len; i++)
{
printf("%02x ", p2[i]);
}
printf("\n");
//===2======16进制转16进制ASCII测试===============//
unsigned char ascii1[30]={0};
unsigned char ascii2[30]={0};
int ascii1_len = 7;
int ascii2_len = 7;
memcpy(ascii1, "\x00\x03\x00\x20\x00\x02\xdb", ascii1_len);
memcpy(ascii2, "\x00\x03\x00\x20\x00\x02\xdb", ascii2_len);
hex2ascii_lowercase(ascii1, &ascii1_len);
printf("ascii1_len:%d,%02x\n", ascii1_len, ascii1_len);
for(int i = 0; i< ascii1_len; i++)
{
printf("%02x ", ascii1[i]);
}
printf("\n");
hex2ascii_biggercase(ascii2, &ascii2_len);
printf("ascii2_len:%d,%02x\n", ascii2_len, ascii2_len);
for(int i = 0; i< ascii2_len; i++)
{
printf("%02x ", ascii2[i]);
}
printf("\n");
return 0;
}
相关推荐
- sharding-jdbc实现`分库分表`与`读写分离`
-
一、前言本文将基于以下环境整合...
- 三分钟了解mysql中主键、外键、非空、唯一、默认约束是什么
-
在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据存储的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的。每一行代表一条唯一的记录,每一列代表记录中的一个域。...
- MySQL8行级锁_mysql如何加行级锁
-
MySQL8行级锁版本:8.0.34基本概念...
- mysql使用小技巧_mysql使用入门
-
1、MySQL中有许多很实用的函数,好好利用它们可以省去很多时间:group_concat()将取到的值用逗号连接,可以这么用:selectgroup_concat(distinctid)fr...
- MySQL/MariaDB中如何支持全部的Unicode?
-
永远不要在MySQL中使用utf8,并且始终使用utf8mb4。utf8mb4介绍MySQL/MariaDB中,utf8字符集并不是对Unicode的真正实现,即不是真正的UTF-8编码,因...
- 聊聊 MySQL Server 可执行注释,你懂了吗?
-
前言MySQLServer当前支持如下3种注释风格:...
- MySQL系列-源码编译安装(v5.7.34)
-
一、系统环境要求...
- MySQL的锁就锁住我啦!与腾讯大佬的技术交谈,是我小看它了
-
对酒当歌,人生几何!朝朝暮暮,唯有己脱。苦苦寻觅找工作之间,殊不知今日之事乃我心之痛,难道是我不配拥有工作嘛。自面试后他所谓的等待都过去一段时日,可惜在下京东上的小金库都要见低啦。每每想到不由心中一...
- MySQL字符问题_mysql中字符串的位置
-
中文写入乱码问题:我输入的中文编码是urf8的,建的库是urf8的,但是插入mysql总是乱码,一堆"???????????????????????"我用的是ibatis,终于找到原因了,我是这么解决...
- 深圳尚学堂:mysql基本sql语句大全(三)
-
数据开发-经典1.按姓氏笔画排序:Select*FromTableNameOrderByCustomerNameCollateChinese_PRC_Stroke_ci_as//从少...
- MySQL进行行级锁的?一会next-key锁,一会间隙锁,一会记录锁?
-
大家好,是不是很多人都对MySQL加行级锁的规则搞的迷迷糊糊,一会是next-key锁,一会是间隙锁,一会又是记录锁。坦白说,确实还挺复杂的,但是好在我找点了点规律,也知道如何如何用命令分析加...
- 一文讲清怎么利用Python Django实现Excel数据表的导入导出功能
-
摘要:Python作为一门简单易学且功能强大的编程语言,广受程序员、数据分析师和AI工程师的青睐。本文系统讲解了如何使用Python的Django框架结合openpyxl库实现Excel...
- 用DataX实现两个MySQL实例间的数据同步
-
DataXDataX使用Java实现。如果可以实现数据库实例之间准实时的...
- MySQL数据库知识_mysql数据库基础知识
-
MySQL是一种关系型数据库管理系统;那废话不多说,直接上自己以前学习整理文档:查看数据库命令:(1).查看存储过程状态:showprocedurestatus;(2).显示系统变量:show...
- 如何为MySQL中的JSON字段设置索引
-
背景MySQL在2015年中发布的5.7.8版本中首次引入了JSON数据类型。自此,它成了一种逃离严格列定义的方式,可以存储各种形状和大小的JSON文档,例如审计日志、配置信息、第三方数据包、用户自定...
你 发表评论:
欢迎- 一周热门
-
-
MySQL中这14个小玩意,让人眼前一亮!
-
旗舰机新标杆 OPPO Find X2系列正式发布 售价5499元起
-
【VueTorrent】一款吊炸天的qBittorrent主题,人人都可用
-
面试官:使用int类型做加减操作,是线程安全吗
-
C++编程知识:ToString()字符串转换你用正确了吗?
-
【Spring Boot】WebSocket 的 6 种集成方式
-
PyTorch 深度学习实战(26):多目标强化学习Multi-Objective RL
-
pytorch中的 scatter_()函数使用和详解
-
与 Java 17 相比,Java 21 究竟有多快?
-
基于TensorRT_LLM的大模型推理加速与OpenAI兼容服务优化
-
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)