-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendian.hpp
87 lines (69 loc) · 3.15 KB
/
endian.hpp
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
// a c++ friendlier version of endian.h provided by most *nix systems
#pragma once
#include <stdint.h>
#include <arpa/inet.h> //hton & ntoh
#include <endian.h>
namespace jdd{
//host to big endian (unsigned)
inline uint8_t htobe(uint8_t i) {return i;}
inline uint16_t htobe(uint16_t i) {return htobe16(i);}
inline uint32_t htobe(uint32_t i) {return htobe32(i);}
inline uint64_t htobe(uint64_t i) {return htobe64(i);}
//big endian to host (unsigned)
inline uint8_t betoh(uint8_t i) {return i;}
inline uint16_t betoh(uint16_t i) {return be16toh(i);}
inline uint32_t betoh(uint32_t i) {return be32toh(i);}
inline uint64_t betoh(uint64_t i) {return be64toh(i);}
//host to big endian (signed)
inline int8_t htobe(int8_t i) {return i;}
inline int16_t htobe(int16_t i) {return htobe16(i);}
inline int32_t htobe(int32_t i) {return htobe32(i);}
inline int64_t htobe(int64_t i) {return htobe64(i);}
//big endian to host (signed)
inline int8_t betoh(int8_t i) {return i;}
inline int16_t betoh(int16_t i) {return be16toh(i);}
inline int32_t betoh(int32_t i) {return be32toh(i);}
inline int64_t betoh(int64_t i) {return be64toh(i);}
//------------------------------------------------------------------------------
//host to little endian (unsigned)
inline uint8_t htole(uint8_t i) {return i;}
inline uint16_t htole(uint16_t i) {return htole16(i);}
inline uint32_t htole(uint32_t i) {return htole32(i);}
inline uint64_t htole(uint64_t i) {return htole64(i);}
//little endian to host (unsigned)
inline uint8_t letoh(uint8_t i) {return i;}
inline uint16_t letoh(uint16_t i) {return le16toh(i);}
inline uint32_t letoh(uint32_t i) {return le32toh(i);}
inline uint64_t letoh(uint64_t i) {return le64toh(i);}
//host to little endian (signed)
inline int8_t htole(int8_t i) {return i;}
inline int16_t htole(int16_t i) {return htole16(i);}
inline int32_t htole(int32_t i) {return htole32(i);}
inline int64_t htole(int64_t i) {return htole64(i);}
//little endian to host (signed)
inline int8_t letoh(int8_t i) {return i;}
inline int16_t letoh(int16_t i) {return le16toh(i);}
inline int32_t letoh(int32_t i) {return le32toh(i);}
inline int64_t letoh(int64_t i) {return le64toh(i);}
//------------------------------------------------------------------------------
//host to network(TCP/UDP) (unsigned)
inline uint8_t hton(uint8_t i) {return i;}
inline uint16_t hton(uint16_t i) {return htons(i);}
inline uint32_t hton(uint32_t i) {return htonl(i);}
inline uint64_t hton(uint64_t i) {return htobe(i);}
//network(TCP/UDP) to host (unsigned)
inline uint8_t ntoh(uint8_t i) {return i;}
inline uint16_t ntoh(uint16_t i) {return ntohs(i);}
inline uint32_t ntoh(uint32_t i) {return ntohl(i);}
inline uint64_t ntoh(uint64_t i) {return betoh(i);}
//host to network(TCP/UDP) (signed)
inline int8_t hton(int8_t i) {return i;}
inline int16_t hton(int16_t i) {return htons(i);}
inline int32_t hton(int32_t i) {return htonl(i);}
inline int64_t hton(int64_t i) {return htobe(i);}
//network(TCP/UDP) to host (signed)
inline int8_t ntoh(int8_t i) {return i;}
inline int16_t ntoh(int16_t i) {return ntohs(i);}
inline int32_t ntoh(int32_t i) {return ntohl(i);}
inline int64_t ntoh(int64_t i) {return betoh(i);}
} // end namespace