网站首页 > 文章精选 正文
/*
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;
}
- 上一篇: C语言编程中的8位、16位、32位整数的分解与合并
- 下一篇: 主机字节序和网络字节序
猜你喜欢
- 2025-01-05 PHP源码系列之扩展的原理与开发
- 2025-01-05 「linux」多个套接字可以绑定同一个端口吗
- 2025-01-05 基于netmap的用户态协议栈(一)
- 2025-01-05 Linux文件:超级块inode dentry file关系
- 2025-01-05 实战Netty!基于私有协议,怎样快速开发网络通信服务
- 2025-01-05 char, unsigned char,之间的相互转换
- 2025-01-05 PHP 扩展与 ZEND 引擎的整合
- 2025-01-05 C语言:位域和字节序
- 2025-01-05 Nor Flash的两种规范
- 2025-01-05 「技术干货」Ip头udp数据包ARP协议(代码实现netmap)
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 稳压管的稳压区是工作在什么区 (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)