百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

16进制和ASCII的互相转换

ztj100 2024-11-24 01:32 13 浏览 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;

}

相关推荐

告别手动操作:一键多工作表合并的实用方法

通常情况下,我们需要将同一工作簿内不同工作表中的数据进行合并处理。如何快速有效地完成这些数据的整合呢?这主要取决于需要合并的源数据的结构。...

【MySQL技术专题】「优化技术系列」常用SQL的优化方案和技术思路

概述前面我们介绍了MySQL中怎么样通过索引来优化查询。日常开发中,除了使用查询外,我们还会使用一些其他的常用SQL,比如INSERT、GROUPBY等。对于这些SQL语句,我们该怎么样进行优化呢...

9.7寸视网膜屏原道M9i双系统安装教程

泡泡网平板电脑频道4月17日原道M9i采用Win8安卓双系统,对于喜欢折腾的朋友来说,刷机成了一件难事,那么原道M9i如何刷机呢?下面通过详细地图文,介绍原道M9i的刷机操作过程,在刷机的过程中,要...

如何做好分布式任务调度——Scheduler 的一些探索

作者:张宇轩,章逸,曾丹初识Scheduler找准定位:分布式任务调度平台...

mysqldump备份操作大全及相关参数详解

mysqldump简介mysqldump是用于转储MySQL数据库的实用程序,通常我们用来迁移和备份数据库;它自带的功能参数非常多,文中列举出几乎所有常用的导出操作方法,在文章末尾将所有的参数详细说明...

大厂面试冲刺,Java“实战”问题三连,你碰到了哪个?

推荐学习...

亿级分库分表,如何丝滑扩容、如何双写灰度

以下是基于亿级分库分表丝滑扩容与双写灰度设计方案,结合架构图与核心流程说明:一、总体设计目标...

MYSQL表设计规范(mysql表设计原则)

日常工作总结,不是通用规范一、表设计库名、表名、字段名必须使用小写字母,“_”分割。...

怎么解决MySQL中的Duplicate entry错误?

在使用MySQL数据库时,我们经常会遇到Duplicateentry错误,这是由于插入或更新数据时出现了重复的唯一键值。这种错误可能会导致数据的不一致性和完整性问题。为了解决这个问题,我们可以采取以...

高并发下如何防重?(高并发如何防止重复)

前言最近测试给我提了一个bug,说我之前提供的一个批量复制商品的接口,产生了重复的商品数据。...

性能压测数据告诉你MySQL和MariaDB该怎么选

1.压测环境为了尽可能的客观公正,本次选择同一物理机上的两台虚拟机,一台用作数据库服务器,一台用作运行压测工具mysqlslap,操作系统均为UbuntuServer22.04LTS。...

屠龙之技 --sql注入 不值得浪费超过十天 实战中sqlmap--lv 3通杀全国

MySQL小结发表于2020-09-21分类于知识整理阅读次数:本文字数:67k阅读时长≈1:01...

破防了,谁懂啊家人们:记一次 mysql 问题排查

作者:温粥一、前言谁懂啊家人们,作为一名java开发,原来以为mysql这东西,写写CRUD,不是有手就行吗;你说DDL啊,不就是设计个表结构,搞几个索引吗。...

SpringBoot系列Mybatis之批量插入的几种姿势

...

MySQL 之 Performance Schema(mysql安装及配置超详细教程)

MySQL之PerformanceSchema介绍PerformanceSchema提供了在数据库运行时实时检查MySQL服务器的内部执行情况的方法,通过监视MySQL服务器的事件来实现监视内...

取消回复欢迎 发表评论: