如何拆分一个ASCII字符的十六进制字节 [英] How to split hex byte of an ASCII character

查看:143
本文介绍了如何拆分一个ASCII字符的十六进制字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上什么我想要做的就是

有关如:'A'六角equivalant是 0x61 ,我可以拆分 61 6 1 ,并将其保存为'6' 1

一个缓冲区这样接收数据:

  rx_dataframe.data [0]为H'00,'。'// H'是十六进制equivalant和''是ASCII值
rx_dataframe.data [0]是H'31,'1'
rx_dataframe.data [0]是H'32,'2'
rx_dataframe.data [0]是H'33,'3'

我需要转换进制数值0x00,0x31,0x32,0x33 中char值 0,0,3 ','1','3','2';'3','3'
并将它们存储在 tx_buff_data []的位置;

我希望我的 tx_buff_data 这个样子

  tx_buff_data [0]有H'30,0
tx_buff_data [1]具有H'30,'0'
tx_buff_data [2]具有H'33,'3'
tx_buff_data [3]具有H'31,'1'
tx_buff_data [4]有H'33,'3'
tx_buff_data [5]具有H'32,'2'
tx_buff_data [6]具有H'33,'3'
tx_buff_data [7]具有H'33,'3'


解决方案

您可以将每个字节分为使用逐两个半字节(4位数量)和+变化:

  unsigned char型LO =字节和放大器;为0x0F;
unsigned char型HI =(字节>> 4)及为0x0F;

然后,您可以各占一半转换成由阵列查找一个十六进制字符(因为字符串本身就是一些字符数组):

 字符loChar =0123456789ABCDEF[LO]
焦炭hiChar =0123456789ABCDEF[喜]

What basically i want to do is

For eg: 'a' hex equivalant is 0x61, can i split61 in to 6 and 1 and store them as '6' and '1' ?

A buffer is receiving data like this:

rx_dataframe.data[0] is H'00,'.'// H' is Hex equivalant and '' is ASCII value
rx_dataframe.data[0] is H'31,'1'
rx_dataframe.data[0] is H'32,'2'
rx_dataframe.data[0] is H'33,'3'

I need to to convert hex values 0x00,0x31,0x32,0x33 in to char value '0','0','3','1','3','2';'3','3' and to store them at the locations of tx_buff_data[];

I want my tx_buff_data look like this

tx_buff_data[0] have H'30,'0'
tx_buff_data[1] have H'30,'0'
tx_buff_data[2] have H'33,'3'
tx_buff_data[3] have H'31,'1'
tx_buff_data[4] have H'33,'3'
tx_buff_data[5] have H'32,'2'
tx_buff_data[6] have H'33,'3'
tx_buff_data[7] have H'33,'3'

解决方案

You can split each byte into two nibbles (4-bit quantities) using bitwise AND + shifts:

unsigned char lo = byte & 0x0f;
unsigned char hi = (byte >> 4) & 0x0f;

Then, you can convert each half into a hex character by an array lookup (since strings are just character arrays):

char loChar = "0123456789ABCDEF"[lo];
char hiChar = "0123456789ABCDEF"[hi];

这篇关于如何拆分一个ASCII字符的十六进制字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆