Cuberite
A lightweight, fast and extensible game server for Minecraft
Endianness.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #undef ntohll
5 #define ntohll(x) (((static_cast<UInt64>(ntohl(static_cast<UInt32>(x)))) << 32) + ntohl(x >> 32))
6 
7 
8 
9 
10 
11 // Changes endianness
12 inline UInt64 HostToNetwork8(const void * a_Value)
13 {
14  UInt64 buf;
15  memcpy( &buf, a_Value, sizeof( buf));
16  buf = (( ( static_cast<UInt64>(htonl(static_cast<UInt32>(buf)))) << 32) + htonl(buf >> 32));
17  return buf;
18 }
19 
20 
21 
22 
23 
24 inline UInt32 HostToNetwork4(const void * a_Value)
25 {
26  UInt32 buf;
27  memcpy( &buf, a_Value, sizeof( buf));
28  buf = ntohl( buf);
29  return buf;
30 }
31 
32 
33 
34 
35 
36 inline double NetworkToHostDouble8(const void * a_Value)
37 {
38  UInt64 buf = 0;
39  memcpy(&buf, a_Value, 8);
40  buf = ntohll(buf);
41  double x;
42  memcpy(&x, &buf, sizeof(double));
43  return x;
44 }
45 
46 
47 
48 
49 
50 inline Int64 NetworkToHostLong8(const void * a_Value)
51 {
52  UInt64 buf;
53  memcpy(&buf, a_Value, 8);
54  buf = ntohll(buf);
55  return *reinterpret_cast<Int64 *>(&buf);
56 }
57 
58 
59 
60 
61 
62 inline UInt64 NetworkToHostULong8(const void * a_Value)
63 {
64  UInt64 buf;
65  memcpy(&buf, a_Value, 8);
66  buf = ntohll(buf);
67  return buf;
68 }
69 
70 
71 
72 
73 
74 inline float NetworkToHostFloat4(const void * a_Value)
75 {
76  UInt32 buf;
77  float x;
78  memcpy(&buf, a_Value, 4);
79  buf = ntohl(buf);
80  memcpy(&x, &buf, sizeof(float));
81  return x;
82 }
83 
84 
85 
86 
NetworkToHostDouble8
double NetworkToHostDouble8(const void *a_Value)
Definition: Endianness.h:36
NetworkToHostLong8
Int64 NetworkToHostLong8(const void *a_Value)
Definition: Endianness.h:50
UInt32
unsigned int UInt32
Definition: Globals.h:154
ntohll
#define ntohll(x)
Definition: Endianness.h:5
HostToNetwork8
UInt64 HostToNetwork8(const void *a_Value)
Definition: Endianness.h:12
Int64
signed long long Int64
Definition: Globals.h:148
HostToNetwork4
UInt32 HostToNetwork4(const void *a_Value)
Definition: Endianness.h:24
NetworkToHostULong8
UInt64 NetworkToHostULong8(const void *a_Value)
Definition: Endianness.h:62
NetworkToHostFloat4
float NetworkToHostFloat4(const void *a_Value)
Definition: Endianness.h:74
UInt64
unsigned long long UInt64
Definition: Globals.h:153