C语言使用springf函数将十六进制数转化为字符类型
功能:将十六进制数转化为字符
#include<stdlib.h>
#include<stdio.h>
typedef unsigned char uint8_t;
char data[6];
uint8_t get[2] = {0x11,0x22};
uint8_t * hex_to_ascii(uint8_t *str, uint8_t len)
{
uint8_t *hex_buf = str;
for (int i = 0; i < len; i++)
{
sprintf(&data[i * 2], "%02X", hex_buf[i]);
}
for (int j = 0; j < 4; j++)
{
/* code */
printf("char :%c\r\n",data[j]);
}
return (uint8_t *)data;
}
void main()
{
/*uint8_t * put[6];
* put = hex_to_ascii(get,2);
for (int j = 0; j < 4; j++)
{
printf("char1 :%c\r\n",*put[j]);
}*/
char data[12];
uint8_t get[6] = {0xFB,0x41,0x18,0x37,0xE3,0xF6};
uint8_t put[17];
for (int i = 0; i < 6; i++)
{
sprintf(&data[i * 2], "%02X", get[i]);
}
for (int j = 0; j < 12; j++)
{
/* code */
printf("char :%c\r\n",data[j]);
}
for (int m=0, n = 5; n<17 & m<12; m++,n++)
{
/* code */
put[n] = data[m];
}
for (int k = 5; k < 17; k++)
{
/* code */
printf("char1 :%c\r\n",put[k]);
}
system("pause");
}