What is the method of converting hexadecimal characters into ASCII characters in C language?
This article "C language hexadecimal and ASCII character conversion method is what" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can read this article can be rewarded, let's take a look at this "C language hexadecimal and ASCII character conversion method is what" article.
1. Convert the obtained hexadecimal data into ASCII characters of the same character, for example: get [2] = {0x11recover0x22}; → put [4] = {'1Chronome2Q 2x2}
Data conversion is mainly done through sprintf ();. There are many explanations for this function on the web. If you want to know more, you can check it out.
Typedef unsigned char uint8_t;char data [64]; uint8_t * hex_to_ascii (uint8_t * str, uint32_t len) {uint8_t * hex_buf = str; for (int I = 0; I
< len; i++) { sprintf(&data[i * 2], "X", hex_buf[i]); } return (uint8_t *)data;} 2、 把获取到的ASCII字符转换成相同字符的16进制,例:get[4]={'a','a','b','b'}; → put[2] = {0xaa,0xbb}; ASCII转换成16进制主要通过sscanf();来完成的。 typedef unsigned char uint8_t;char data[64];uint8_t *ascii_to_hex(char *str, uint32_t len){ char *str_buf = str; uint8_t *hex = (uint8_t *)data; uint8_t data_buf[len]; for (int i = 0; i < len; i++) { sscanf(str_buf, "x", (int *)&data_buf[i]); str_buf += 2; } memcpy(hex, data_buf, len); return hex;} 3、把获取到的16进制转换成对应的ASCII字符,例:get[4]={'a','a','b','b'}; → put[4]={0x61,0x61,0x62,0x62}; typedef unsigned char uint8_t;char data[64];uint8_t *Char_to_Hex(char *str,uint32_t len){ char *hex_buf = str; uint8_t *hex = (uint8_t *)data; for (int i = 0; i < len; i++) { if ((hex_buf[i] >='0') & (hex_ BUF [I] ='a') & & (hex_ BUF [I] ='A') & & (hex_ BUF [I] = 0x30) & & (hex_ BUF [I] = 0x41) & (hex_ BUF [I] = 0x61) & (hex_ BUF [I]