I recently looked at network programming. Since the TCP protocol is based on byte streams, I suddenly thought of how to perform the hexadecimal output of variables. So I summarized this article.
The example is given in C language.
First, let's talk about my programming environment. In windows, I used vs2005 for compiling, And the CPU was Intel's P4, so the bytes of those variables were:
Char is 1 byte short is 2 byte
Int, long, float, pointer is 4-byte double, long is 8 bytes
Conversion of string multi-byte encoding and Unicode encoding in VC is performed in the "Project -- properties -- configuration properties -- General -- (right) Character Set" option.
Because tchar and text macros are used, the header file # include <windows. h> must be added.
First look at the following functions:
When copying the following code to vs2005, you may copy the number on the left. In this case, use "edit-search and replace" in vs2005 to switch to "quick Replace ", first, check "use regular expression" of "search options", and then enter the content in double quotation marks (not double quotation marks) on "search content ): "^ + [0-9] + /. "," Replace with "does not fill in anything, and then click" replace "to replace one by one. (Sorry, the language is too bad, and the expression is too long. I smiled at everyone .)
1. typedef unsigned char uchar;
2.
3. Void printhex (uchar * STR, int size)
4.5 .{
6. Int Index = 0;
7.8. For (Index = 0; index <size; index ++)
9.10./* % x is output in hexadecimal format */
11.
12. printf ("% x", STR [Index]);
13.14. printf ("/N ");
15.16 .}
Let's look at the following call example:
1. Char STR [] = "ABCDE ";
2.3.printhex (uchar *) STR, sizeof (STR ));
4.
5.
6./* multi-byte encoding (gb2312 or GBK) in both Chinese and English */7.8.tchar ansi_str [] = text ("I am ABC ");
9.10.printhex (uchar *) ansi_str, sizeof (ansi_str ));
11.
12.
13./* combination of Chinese and English characters in Unicode encoding */14.15.tchar uni_str [] = text ("I am ABC ");
16.17.printhex (uchar *) uni_str, sizeof (uni_str ));
18.
19.
20. int A = 123456;
21.22.printhex (uchar *) & A, sizeof ());
23.
24.
25. Long lg = 1234567899123;
26.27.printhex (uchar *) & lg, sizeof (LG ));
28.
29.
30. Float F = 123.456f;
31.32.printhex (uchar *) & F, sizeof (f ));
33.
34.
35. Double D = 123.45678;
36.37.printhex (uchar *) & D, sizeof (d ));
38.
39.
40. struct
41.42 .{
43. int;
44.45. Char B;
46.47. Float C;
48 .};
49.50.struct a AA;
51.52.aa.a = 123457;
53.54.aa. B = 'C ';
55.56.aa.c = 123.456f;
57.58.printhex (uchar *) & aa, sizeof (AA ));
59.
Next let's take a look at the output result. The memory address is increased from left to right for convenience. For example, output 4 is written as 04 and Output A is written as 0a.
1./* char STR [] = "ABCDE" */2.3.61 62 63 64 65 00
4.
5./* multi-byte encoding of tchar ansi_str [] = "I am ABC" */6.7.ce D2 ca C7 41 42 63 00
8.
9./* tchar uni_str [] = "I am ABC "*/
10.11.11 62 2f 66 41 00 42 00 63 00 00
12.
13./* int A = 123456 */14.15.40 E2 01 00
16.
17./* long lg = 1234567898123 */
18.19.f3 27 FB 71 1f 01 00 00
20.
21./* float F = 123.456f */
22.23.79 E9 F6 42
24.
25./* Double D = 123.45678 */
26.27.e1 5d 2E E2 3B dd 5E 40
28.
29./* struct a aa = [(INT) 123457, (char) 'C', (float)
30.31.123.456f] */
32.33.41 E2 01 00 63 CC 79 E9 F6 42
The following questions are to be discussed:
1. Why use unsigned char as a pseudo-input parameter instead of Char? (Details of character encoding)
2. What are the differences between multibyte encoding and unicde encoding output?
3. int A = 123456; The hexadecimal number is 0x1e240. Why is the output 40 E2 01 00?
(Details about big endian and little endian are introduced)
4. How is the output of Float F = 123.456f obtained? (Memory format for floating point numbers)
5. The aA member of the variable struct a occupies 9 bytes (4 + 1 + 4 = 9). Why is the output 12 bytes? (Extracts the byte alignment of the struct)
In the following sections: Question 1 and 2 are used together to write an article. Question 3, 4, and question 5 are each written in one article.
Work first, and write it later.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jiangnanyouzi/archive/2008/10/22/3122344.aspx