-
Notifications
You must be signed in to change notification settings - Fork 1
/
big_or_little_endian_func.cpp
215 lines (187 loc) · 4.04 KB
/
big_or_little_endian_func.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <exception>
#include <process.h>
using namespace std;
/**
** 源文件:big_or_little_endian_func.cpp
** 功能描述:提供字节序转化的基本函数,包括判断本机字节序高端或低端功能。
** ** 包含测试程序。
**
** 作者:junkun huang e-mail:[email protected]
** 日期:2010-11-25 /
*/
namespace test_namespace
{
static int Hash(const char *z, int n)
{
int h = 0;
if( n<=0 )
{
n = strlen(z);
}
while( n-- )
{
h = h ^ (h<<5) ^ *z++;
}
if( h<0 ) h = -h;
return h;
}
template <typename T>
class test_t
{
public:
static T s_value;
};
template <typename T>
T test_t<T>::s_value = 0;
}
namespace test_namespace
{
bool _TestBigEndian()
{
union UnionForTest
{
short _short;
char _char;
};
UnionForTest test;
test._short = 0x0100;
return 1 == (int)test._char;
}
bool TestBigEndian()
{
static bool s_is_big_endian = _TestBigEndian();
return s_is_big_endian;
}
bool _TestLittleEndian()
{
union UnionForTest
{
short _short;
char _char;
};
UnionForTest test;
test._short = 0x0001;
return 1 == (int)test._char;
}
bool TestLittleEndian()
{
static bool s_is_little_endian = _TestLittleEndian();
return s_is_little_endian;
}
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned __int64 uint64;
uint16 htons(uint16 n)
{
if (TestBigEndian())
return n;
return ((n & 0xff) << 8) |
((n & 0xff00) >> 8);
}
uint16 ntohs(uint16 n)
{
return htons(n);
}
uint32 htonl(uint32 n)
{
if (TestBigEndian())
return n;
return ((n & 0xff) << 24) |
((n & 0xff00) << 8) |
((n & 0xff0000) >> 8) |
((n & 0xff000000) >> 24);
}
uint32 ntohl(uint32 n)
{
return htonl(n);
}
uint64 htonll(uint64 n)
{
if (TestBigEndian())
return n;
return ((n & 0xff) << 56) |
((n & 0xff00) << 40) |
((n & 0xff0000) << 24) |
((n & 0xff000000) << 8) |
((n & 0xff00000000LL) >> 8) |
((n & 0xff0000000000LL) >> 24) |
((n & 0xff000000000000LL) >> 40) |
((n & 0xff00000000000000LL) >> 56);
}
uint64 ntohll(uint64 n)
{
return htonll(n);
}
uint8 hton(uint8 n)
{
return n;
}
uint16 hton(uint16 n)
{
return htons(n);
}
uint32 hton(uint32 n)
{
return htonl(n);
}
uint64 hton(uint64 n)
{
return htonll(n);
}
uint8 ntoh(uint8 n)
{
return n;
}
uint16 ntoh(uint16 n)
{
return ntohs(n);
}
uint32 ntoh(uint32 n)
{
return ntohl(n);
}
uint64 ntoh(uint64 n)
{
return ntohll(n);
}
} // namespace test_namespace
int main()
{
///*
{
using namespace test_namespace;
uint8 a_uint8 = 0x12;
uint16 a_uint16 = 0x1234;
uint32 a_uint32 = 0x12345678;
uint64 a_uint64 = 0x1234567890abcdef;
std::cout << " " << TestBigEndian() << "\n";
std::cout << hex << a_uint8 << "\n";
std::cout << hex << a_uint16 << "\n";
std::cout << hex << a_uint32 << "\n";
std::cout << hex << a_uint64 << "\n";
std::cout << hex << hton(a_uint8) << "\n";
std::cout << hex << hton(a_uint16) << "\n";
std::cout << hex << hton(a_uint32) << "\n";
std::cout << hex << hton(a_uint64) << "\n";
}
/*
{
using namespace TestSpace;
cout << " test_big_endian:" << test_big_endian() << " ,test_little_endian:" << test_little_endian() << "\n";
cout << " test_big_endian:" << test_big_endian() << " ,test_little_endian:" << test_little_endian() << "\n";
cout << " test_big_endian:" << test_big_endian() << " ,test_little_endian:" << test_little_endian() << "\n";
}
/** =========================*/
cout << "Hello world!" << endl;
return 0;
}