网络字节序(Big Endian)
网络字节序(Big Endian)
test.c
-------------------------------------
01 #include <stdio.h>
02 #include <arpa/inet.h>
03
04 int main()
05 {
06 unsigned short name = 0x123;
07 unsigned char * p = (unsigned char *)&name;
08 printf("p[0] = 0x%x\n", p[0]);
09 printf("p[1] = 0x%x\n", p[1]);
10
11 printf("htons(0x%x) = 0x%x\n", name, htons(name));
12
13 name = htons(name);
14 p = (unsigned char *)&name;
15 printf("p[0] = 0x%x\n", p[0]);
16 printf("p[1] = 0x%x\n", p[1]);
17
18 return 0;
19 }
# gcc test.c
# ./a.out
p[0] = 0x23
p[1] = 0x1
htons(0x123) = 0x2301
p[0] = 0x1
p[1] = 0x23
网络字节序(Big Endian)
起始地址
|------|------|
| 0x01 | 0x23 |
|------|------|
X86体系结构(Little Endian)
起始地址
|------|------|
| 0x23 | 0x01 |
|------|------|
注: 字节序 not 位序
test.c
-------------------------------------
01 #include <stdio.h>
02 #include <arpa/inet.h>
03
04 int main()
05 {
06 unsigned int name = 0x01234567;
07 unsigned char * p = (unsigned char *)&name;
08 printf("p[0] = 0x%x\n", p[0]);
09 printf("p[1] = 0x%x\n", p[1]);
10 printf("p[2] = 0x%x\n", p[2]);
11 printf("p[3] = 0x%x\n", p[3]);
12
13 printf("htols(0x%x) = 0x%x\n", name, htonl(name));
14
15 name = htonl(name);
16 p = (unsigned char *)&name;
17 printf("p[0] = 0x%x\n", p[0]);
18 printf("p[1] = 0x%x\n", p[1]);
19 printf("p[2] = 0x%x\n", p[2]);
20 printf("p[3] = 0x%x\n", p[3]);
21
22 return 0;
23 }
# gcc test.c
# ./a.out
p[0] = 0x67
p[1] = 0x45
p[2] = 0x23
p[3] = 0x1
htols(0x1234567) = 0x67452301
p[0] = 0x1
p[1] = 0x23
p[2] = 0x45
p[3] = 0x67
网络字节序(Big Endian)
起始地址
|------|------|------|------|
| 0x01 | 0x23 | 0x45 | 0x67 |
|------|------|------|------|
X86体系结构(Little Endian)
起始地址
|------|------|------|------|
| 0x67 | 0x45 | 0x23 | 0x01 |
|------|------|------|------|
-------------------------------------
01 #include <stdio.h>
02 #include <arpa/inet.h>
03
04 int main()
05 {
06 unsigned short name = 0x123;
07 unsigned char * p = (unsigned char *)&name;
08 printf("p[0] = 0x%x\n", p[0]);
09 printf("p[1] = 0x%x\n", p[1]);
10
11 printf("htons(0x%x) = 0x%x\n", name, htons(name));
12
13 name = htons(name);
14 p = (unsigned char *)&name;
15 printf("p[0] = 0x%x\n", p[0]);
16 printf("p[1] = 0x%x\n", p[1]);
17
18 return 0;
19 }
# gcc test.c
# ./a.out
p[0] = 0x23
p[1] = 0x1
htons(0x123) = 0x2301
p[0] = 0x1
p[1] = 0x23
网络字节序(Big Endian)
起始地址
|------|------|
| 0x01 | 0x23 |
|------|------|
X86体系结构(Little Endian)
起始地址
|------|------|
| 0x23 | 0x01 |
|------|------|
注: 字节序 not 位序
test.c
-------------------------------------
01 #include <stdio.h>
02 #include <arpa/inet.h>
03
04 int main()
05 {
06 unsigned int name = 0x01234567;
07 unsigned char * p = (unsigned char *)&name;
08 printf("p[0] = 0x%x\n", p[0]);
09 printf("p[1] = 0x%x\n", p[1]);
10 printf("p[2] = 0x%x\n", p[2]);
11 printf("p[3] = 0x%x\n", p[3]);
12
13 printf("htols(0x%x) = 0x%x\n", name, htonl(name));
14
15 name = htonl(name);
16 p = (unsigned char *)&name;
17 printf("p[0] = 0x%x\n", p[0]);
18 printf("p[1] = 0x%x\n", p[1]);
19 printf("p[2] = 0x%x\n", p[2]);
20 printf("p[3] = 0x%x\n", p[3]);
21
22 return 0;
23 }
# gcc test.c
# ./a.out
p[0] = 0x67
p[1] = 0x45
p[2] = 0x23
p[3] = 0x1
htols(0x1234567) = 0x67452301
p[0] = 0x1
p[1] = 0x23
p[2] = 0x45
p[3] = 0x67
网络字节序(Big Endian)
起始地址
|------|------|------|------|
| 0x01 | 0x23 | 0x45 | 0x67 |
|------|------|------|------|
X86体系结构(Little Endian)
起始地址
|------|------|------|------|
| 0x67 | 0x45 | 0x23 | 0x01 |
|------|------|------|------|
评论
发布
posted on 2012-07-21 09:38 java课程设计例子 阅读(284) 评论(0) 编辑 收藏 举报