一、简述
记--C语言实现 文本形式的字符串和十六进制字符串的相互转换。如"01" <==> "3031"
有的数据包含不可见字符,直接打印出来会不可见或残缺,需要转换为十六进制直接看ASCII码值。
二、测试代码
#include
#include
/* ASCII字符 '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'
* ASCII码值 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39
*/
typedef unsigned char uint8_t;
//判断是否是十六进制的基数
int isHexNum(uint8_t _hexNum)
{
if('0'<=_hexNum && _hexNum<='9')
{
return 1;
}
else if('A'<=_hexNum && _hexNum<='F')
{
return 2;
}
else if('a'<=_hexNum && _hexNum<='f')
{
return 3;
}
return -1;
}
void numToHexStr(uint8_t _hexNum, uint8_t* _hexStr)
{
uint8_t tmp;
if(NULL == _hexStr)
{
return;
}
//低4bit
tmp = (_hexNum >> 4) & 0x0f;
if(tmp <= 9)
*_hexStr = tmp+'0';
else
*_hexStr = tmp-0x0a + 'A';
_hexStr++;
//高4bit
tmp = _hexNum & 0x0f;
if(tmp <= 9)
*_hexStr = tmp+'0';
else
*_hexStr = tmp-0x0a + 'A';
}
//将字符串转为16进制形式,以查看不可见字符 "01" ==> "3031"
int toHexStr(const uint8_t* _str, uint8_t *_hexStr)
{
int i;
int len;
uint8_t* resultPtr;
if(NULL == _str || NULL == _hexStr)
{
return -1;
}
len = strlen(_str);
resultPtr = _hexStr;
for(i=0; iisHexNum(hexChar))
{
return 0xFF;
}
if(hexChar<='9')
{
tmp = hexChar-'0';
}
else if(hexChar<='F')
{
tmp = hexChar-'7';
}
else
{
tmp = hexChar-'W';
}
return tmp;
}
//将16进制形式的字符串转为文本形式 "3031" ==> "01"
int hexToStr(const uint8_t* _hexStr, uint8_t* _str)
{
int i;
int len;
uint8_t* ptr;
if(NULL == _str || NULL == _hexStr)
{
return -1;
}
len = strlen(_hexStr);
ptr = _str;
//要是单数个字符,则最后一个会被丢弃
for(i=0; i
三、测试结果
四、附--ASCII表
十进制ASCII码 | |||||||||||||||
ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 |
0 | NUT | 16 | DLE | 32 | 空格 | 48 | 0 | 64 | @ | 80 | P | 96 | 、 | 112 | p |
1 | SOH | 17 | DCI | 33 | ! | 49 | 1 | 65 | A | 81 | Q | 97 | a | 113 | q |
2 | STX | 18 | DC2 | 34 | " | 50 | 2 | 66 | B | 82 | R | 98 | b | 114 | r |
3 | ETX | 19 | DC3 | 35 | # | 51 | 3 | 67 | C | 83 | S | 99 | c | 115 | s |
4 | EOT | 20 | DC4 | 36 | $ | 52 | 4 | 68 | D | 84 | T | 100 | d | 116 | t |
5 | ENQ | 21 | NAK | 37 | % | 53 | 5 | 69 | E | 85 | U | 101 | e | 117 | u |
6 | ACK | 22 | SYN | 38 | & | 54 | 6 | 70 | F | 86 | V | 102 | f | 118 | v |
7 | BEL | 23 | TB | 39 | , | 55 | 7 | 71 | G | 87 | W | 103 | g | 119 | w |
8 | BS | 24 | CAN | 40 | ( | 56 | 8 | 72 | H | 88 | X | 104 | h | 120 | x |
9 | HT | 25 | EM | 41 | ) | 57 | 9 | 73 | I | 89 | Y | 105 | i | 121 | y |
10 | LF | 26 | SUB | 42 | * | 58 | : | 74 | J | 90 | Z | 106 | j | 122 | z |
11 | VT | 27 | ESC | 43 | + | 59 | ; | 75 | K | 91 | [ | 107 | k | 123 | { |
12 | FF | 28 | FS | 44 | , | 60 | < | 76 | L | 92 | / | 108 | l | 124 | | |
13 | CR | 29 | GS | 45 | - | 61 | = | 77 | M | 93 | ] | 109 | m | 125 | } |
14 | SO | 30 | RS | 46 | . | 62 | > | 78 | N | 94 | ^ | 110 | n | 126 | ` |
15 | SI | 31 | US | 47 | / | 63 | ? | 79 | O | 95 | _ | 111 | o | 127 | DEL |
十六进制ASCII码 | |||||||||||||||
ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 | ASCII值 | 控制字符 |
0 | NUT | 10 | DLE | 20 | 空格 | 30 | 0 | 40 | @ | 50 | P | 60 | 、 | 70 | p |
1 | SOH | 11 | DCI | 21 | ! | 31 | 1 | 41 | A | 51 | Q | 61 | a | 71 | q |
2 | STX | 12 | DC2 | 22 | " | 32 | 2 | 42 | B | 52 | R | 62 | b | 72 | r |
3 | ETX | 13 | DC3 | 23 | # | 33 | 3 | 43 | C | 53 | S | 63 | c | 73 | s |
4 | EOT | 14 | DC4 | 24 | $ | 34 | 4 | 44 | D | 54 | T | 64 | d | 74 | t |
5 | ENQ | 15 | NAK | 25 | % | 35 | 5 | 45 | E | 55 | U | 65 | e | 75 | u |
6 | ACK | 16 | SYN | 26 | & | 36 | 6 | 46 | F | 56 | V | 66 | f | 76 | v |
7 | BEL | 17 | TB | 27 | , | 37 | 7 | 47 | G | 57 | W | 67 | g | 77 | w |
8 | BS | 18 | CAN | 28 | ( | 38 | 8 | 48 | H | 58 | X | 68 | h | 78 | x |
9 | HT | 19 | EM | 29 | ) | 39 | 9 | 49 | I | 59 | Y | 69 | i | 79 | y |
0A | LF | 1A | SUB | 2A | * | 3A | : | 4A | J | 5A | Z | 6A | j | 7A | z |
0B | VT | 1B | ESC | 2B | + | 3B | ; | 4B | K | 5B | [ | 6B | k | 7B | { |
0C | FF | 1C | FS | 2C | , | 3C | < | 4C | L | 5C | / | 6C | l | 7C | | |
0D | CR | 1D | GS | 2D | - | 3D | = | 4D | M | 5D | ] | 6D | m | 7D | } |
0E | SO | 1E | RS | 2E | . | 3E | > | 4E | N | 5E | ^ | 6E | n | 7E | ` |
0F | SI | 1F | US | 2F | / | 3F | ? | 4F | O | 5F | _ | 6F | o | 7F | DEL |
二进制ASCII码 | ||||
Bin | Dec | Hex | 缩写/字符 | 解释 |
0 | 0 | 0 | NUL(null) | 空字符 |
1 | 1 | 1 | SOH(start of headling) | 标题开始 |
10 | 2 | 2 | STX (start of text) | 正文开始 |
11 | 3 | 3 | ETX (end of text) | 正文结束 |
100 | 4 | 4 | EOT (end of transmission) | 传输结束 |
101 | 5 | 5 | ENQ (enquiry) | 请求 |
110 | 6 | 6 | ACK (acknowledge) | 收到通知 |
111 | 7 | 7 | BEL (bell) | 响铃 |
1000 | 8 | 8 | BS (backspace) | 退格 |
1001 | 9 | 9 | HT (horizontal tab) | 水平制表符 |
1010 | 10 | 0A | LF (NL line feed, new line) | 换行键 |
1011 | 11 | 0B | VT (vertical tab) | 垂直制表符 |
1100 | 12 | 0C | FF (NP form feed, new page) | 换页键 |
1101 | 13 | 0D | CR (carriage return) | 回车键 |
1110 | 14 | 0E | SO (shift out) | 不用切换 |
1111 | 15 | 0F | SI (shift in) | 启用切换 |
10000 | 16 | 10 | DLE (data link escape) | 数据链路转义 |
10001 | 17 | 11 | DC1 (device control 1) | 设备控制1 |
10010 | 18 | 12 | DC2 (device control 2) | 设备控制2 |
10011 | 19 | 13 | DC3 (device control 3) | 设备控制3 |
10100 | 20 | 14 | DC4 (device control 4) | 设备控制4 |
10101 | 21 | 15 | NAK (negative acknowledge) | 拒绝接收 |
10110 | 22 | 16 | SYN (synchronous idle) | 同步空闲 |
10111 | 23 | 17 | ETB (end of trans. block) | 传输块结束 |
11000 | 24 | 18 | CAN (cancel) | 取消 |
11001 | 25 | 19 | EM (end of medium) | 介质中断 |
11010 | 26 | 1A | SUB (substitute) | 替补 |
11011 | 27 | 1B | ESC (escape) | 溢出 |
11100 | 28 | 1C | FS (file separator) | 文件分割符 |
11101 | 29 | 1D | GS (group separator) | 分组符 |
11110 | 30 | 1E | RS (record separator) | 记录分离符 |
11111 | 31 | 1F | US (unit separator) | 单元分隔符 |
100000 | 32 | 20 | (space) | 空格 |
100001 | 33 | 21 | ! | |
100010 | 34 | 22 | " | |
100011 | 35 | 23 | # | |
100100 | 36 | 24 | $ | |
100101 | 37 | 25 | % | |
100110 | 38 | 26 | & | |
100111 | 39 | 27 | ' | |
101000 | 40 | 28 | ( | |
101001 | 41 | 29 | ) | |
101010 | 42 | 2A | * | |
101011 | 43 | 2B | + | |
101100 | 44 | 2C | , | |
101101 | 45 | 2D | - | |
101110 | 46 | 2E | . | |
101111 | 47 | 2F | / | |
110000 | 48 | 30 | 0 | |
110001 | 49 | 31 | 1 | |
110010 | 50 | 32 | 2 | |
110011 | 51 | 33 | 3 | |
110100 | 52 | 34 | 4 | |
110101 | 53 | 35 | 5 | |
110110 | 54 | 36 | 6 | |
110111 | 55 | 37 | 7 | |
111000 | 56 | 38 | 8 | |
111001 | 57 | 39 | 9 | |
111010 | 58 | 3A | : | |
111011 | 59 | 3B | ; | |
111100 | 60 | 3C | < | |
111101 | 61 | 3D | = | |
111110 | 62 | 3E | > | |
111111 | 63 | 3F | ? | |
1000000 | 64 | 40 | @ | |
1000001 | 65 | 41 | A | |
1000010 | 66 | 42 | B | |
1000011 | 67 | 43 | C | |
1000100 | 68 | 44 | D | |
1000101 | 69 | 45 | E | |
1000110 | 70 | 46 | F | |
1000111 | 71 | 47 | G | |
1001000 | 72 | 48 | H | |
1001001 | 73 | 49 | I | |
1001010 | 74 | 4A | J | |
1001011 | 75 | 4B | K | |
1001100 | 76 | 4C | L | |
1001101 | 77 | 4D | M | |
1001110 | 78 | 4E | N | |
1001111 | 79 | 4F | O | |
1010000 | 80 | 50 | P | |
1010001 | 81 | 51 | Q | |
1010010 | 82 | 52 | R | |
1010011 | 83 | 53 | S | |
1010100 | 84 | 54 | T | |
1010101 | 85 | 55 | U | |
1010110 | 86 | 56 | V | |
1010111 | 87 | 57 | W | |
1011000 | 88 | 58 | X | |
1011001 | 89 | 59 | Y | |
1011010 | 90 | 5A | Z | |
1011011 | 91 | 5B | [ | |
1011100 | 92 | 5C | \ | |
1011101 | 93 | 5D | ] | |
1011110 | 94 | 5E | ^ | |
1011111 | 95 | 5F | _ | |
1100000 | 96 | 60 | ` | |
1100001 | 97 | 61 | a | |
1100010 | 98 | 62 | b | |
1100011 | 99 | 63 | c | |
1100100 | 100 | 64 | d | |
1100101 | 101 | 65 | e | |
1100110 | 102 | 66 | f | |
1100111 | 103 | 67 | g | |
1101000 | 104 | 68 | h | |
1101001 | 105 | 69 | i | |
1101010 | 106 | 6A | j | |
1101011 | 107 | 6B | k | |
1101100 | 108 | 6C | l | |
1101101 | 109 | 6D | m | |
1101110 | 110 | 6E | n | |
1101111 | 111 | 6F | o | |
1110000 | 112 | 70 | p | |
1110001 | 113 | 71 | q | |
1110010 | 114 | 72 | r | |
1110011 | 115 | 73 | s | |
1110100 | 116 | 74 | t | |
1110101 | 117 | 75 | u | |
1110110 | 118 | 76 | v | |
1110111 | 119 | 77 | w | |
1111000 | 120 | 78 | x | |
1111001 | 121 | 79 | y | |
1111010 | 122 | 7A | z | |
1111011 | 123 | 7B | { | |
1111100 | 124 | 7C | | | |
1111101 | 125 | 7D | } | |
1111110 | 126 | 7E | ~ | |
1111111 | 127 | 7F | DEL (delete) | 删除 |