summaryrefslogtreecommitdiffstats
path: root/src/Endianness.h
blob: cfa8593283d2617ebcbabf362a8615fb9a37fd75 (plain) (blame)
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

#pragma once

#undef ntohll
#define ntohll(x) (((static_cast<UInt64>(ntohl(static_cast<UInt32>(x)))) << 32) + ntohl(x >> 32))





// Changes endianness
inline UInt64 HostToNetwork8(const void * a_Value)
{
	UInt64 buf;
	memcpy(&buf, a_Value, sizeof(buf));
	buf = (((static_cast<UInt64>(htonl(static_cast<UInt32>(buf)))) << 32) + htonl(buf >> 32));
	return buf;
}





inline UInt32 HostToNetwork4(const void * a_Value)
{
	UInt32 buf;
	memcpy(&buf, a_Value, sizeof(buf));
	buf = ntohl(buf);
	return buf;
}





inline double NetworkToHostDouble8(const void * a_Value)
{
	UInt64 buf = 0;
	memcpy(&buf, a_Value, 8);
	buf = ntohll(buf);
	double x;
	memcpy(&x, &buf, sizeof(double));
	return x;
}





inline Int64 NetworkToHostLong8(const void * a_Value)
{
	UInt64 buf;
	memcpy(&buf, a_Value, 8);
	buf = ntohll(buf);
	return *reinterpret_cast<Int64 *>(&buf);
}





inline UInt64 NetworkToHostULong8(const void * a_Value)
{
	UInt64 buf;
	memcpy(&buf, a_Value, 8);
	buf = ntohll(buf);
	return buf;
}





inline float NetworkToHostFloat4(const void * a_Value)
{
	UInt32 buf;
	float x;
	memcpy(&buf, a_Value, 4);
	buf = ntohl(buf);
	memcpy(&x, &buf, sizeof(float));
	return x;
}