% 02hhX,
Debugging information, such as printing data packets received from the network or converted data, is often encountered.
If it is printed in ascii code, you cannot view the specific values of characters other than the control character and ascii code (not visible or garbled, especially for network packets ).
If it is printed in hexadecimal format, this problem is avoided.
The output format is often not standard. For a single byte, it sometimes occupies 1 byte space of 7F, and sometimes occupies 8 byte space of fffffe.
Now, let us know a standard format. You don't need to consider whether the memory space is a signed string, unsigned string, or type conversion.
Char * src;
Printf ("% 02hhX", src );
Hh indicates the printed value as a byte char type. Similar to the value printed in a short type using the h flag. (In fact, the size of the memory space is different. If char, short, and int are all unsigned values, the marked values are the same and only occupy different sizes of space)
% X indicates that an integer (int, four bytes) is printed in hexadecimal notation. For an unsigned or signed int, the memory stores the same value, but the values are different.
For example, if the value of a char type is 0xFF, the binary value is 1 for all eight digits, the unsigned value is 255, and the signed value is-1.
The combination of hh and X indicates that an integer is printed as a char type, and the four-byte integer is truncated into one byte.
02 if the front side is less than two digits, 0 is supplemented to two digits for printing. If the front side is more than two digits, the actual length is used for printing,
For % 02hhx, The hhx has been printed in one byte, and the limit of 02 is added. If the limit is less than two, the hhx is supplemented into two.
For:
Char a = '\ xab ';
Printf ("% 02X", a); // ff AB (the highest bit is 1, and 1 is added before symbol extension, ff AB)
Unsigned char a = '\ xab ';
Printf ("% 02X", a); // AB (unsigned char, prefix 0,In fact, the printed value is still four bytes 00 00 AB, but the first is 0, so it is displayed in 2 bits.)
Therefore, % 02hhX printing is preferred.
Note: The above int occupies 4 bytes, which is on a 32-bit system.
% X print hexadecimal with uppercase letters
% X print hexadecimal with lowercase letters
Char a = '\ 12' octal integer 12
Char a = '\ 012' octal integer 12
Char a = '\ x12' hexadecimal integer 12
In linux, there is a hex hexdump Printing file, but pay attention to the byte order.