1 | /* This module provides an equivalent of <netinet/in.h> on */
|
---|
2 | /* unix systems. */
|
---|
3 |
|
---|
4 | unsigned long htonl(unsigned long x) {
|
---|
5 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
6 | x = ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) |
|
---|
7 | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000);
|
---|
8 | #endif
|
---|
9 |
|
---|
10 | return x;
|
---|
11 | }
|
---|
12 |
|
---|
13 | unsigned long ntohl(unsigned long x) {
|
---|
14 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
15 | x = ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) |
|
---|
16 | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000);
|
---|
17 | #endif
|
---|
18 | return x;
|
---|
19 | }
|
---|
20 |
|
---|
21 | unsigned short htons(unsigned short x) {
|
---|
22 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
23 | x = ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
|
---|
24 | #endif
|
---|
25 | return x;
|
---|
26 | }
|
---|
27 |
|
---|
28 | unsigned short ntohs(unsigned short x) {
|
---|
29 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
30 | x = ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
|
---|
31 | #endif
|
---|
32 | return x;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /* just to test stuff
|
---|
36 | #include <stdio.h>
|
---|
37 |
|
---|
38 | main () {
|
---|
39 | long x;
|
---|
40 | short sx;
|
---|
41 |
|
---|
42 | x = 0x000000FF;
|
---|
43 | printf ("before %i, ",(int)x);
|
---|
44 | x = htonl (x);
|
---|
45 | printf ("after %i\n",(int)x);
|
---|
46 |
|
---|
47 | x = 0x0000FF00;
|
---|
48 | printf ("before %i, ",(int)x);
|
---|
49 | x = htonl (x);
|
---|
50 | printf ("after %i\n",(int)x);
|
---|
51 |
|
---|
52 | x = 0x00FF0000;
|
---|
53 | printf ("before %i, ",(int)x);
|
---|
54 | x = htonl (x);
|
---|
55 | printf ("after %i\n",(int)x);
|
---|
56 |
|
---|
57 | x = 0xFF000000;
|
---|
58 | printf ("before %i, ",(int)x);
|
---|
59 | x = htonl (x);
|
---|
60 | printf ("after %i\n\n",(int)x);
|
---|
61 |
|
---|
62 | sx = 0x00FF;
|
---|
63 | printf ("before %i, ",(int)sx);
|
---|
64 | sx = htons (sx);
|
---|
65 | printf ("after %i\n",(int)sx);
|
---|
66 |
|
---|
67 | sx = 0xFF00;
|
---|
68 | printf ("before %i, ",(int)sx);
|
---|
69 | sx = htons (sx);
|
---|
70 | printf ("after %i\n",(int)sx);
|
---|
71 | }
|
---|
72 | */ |
---|