C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
binaryconversionprivate.h
Go to the documentation of this file.
1 #ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
2 #error "Do not include binaryconversionprivate.h directly."
3 #else
4 
5 #include "./types.h"
6 
7 #include "../global.h"
8 
12 CPP_UTILITIES_EXPORT constexpr int16 toInt16(const char *value)
13 {
14 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
15  return (static_cast<int16>(value[0]) << 8 & 0xFF00) | (static_cast<int16>(value[1]) & 0x00FF);
16 #else
17  return (static_cast<int16>(value[1]) << 8 & 0xFF00) | (static_cast<int16>(value[0]) & 0x00FF);
18 #endif
19 }
20 
24 CPP_UTILITIES_EXPORT constexpr uint16 toUInt16(const char *value)
25 {
26 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
27  return (static_cast<uint16>(value[0]) << 8 & 0xFF00) | (static_cast<uint16>(value[1]) & 0x00FF);
28 #else
29  return (static_cast<uint16>(value[1]) << 8 & 0xFF00) | (static_cast<uint16>(value[0]) & 0x00FF);
30 #endif
31 }
32 
36 CPP_UTILITIES_EXPORT constexpr int32 toInt32(const char *value)
37 {
38 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
39  return (static_cast<int32>(value[0]) << 24 & 0xFF000000) | (static_cast<int32>(value[1]) << 16 & 0x00FF0000)
40  | (static_cast<int32>(value[2]) << 8 & 0x0000FF00) | (static_cast<int32>(value[3]) & 0x000000FF);
41 #else
42  return (static_cast<int32>(value[3]) << 24 & 0xFF000000) | (static_cast<int32>(value[2]) << 16 & 0x00FF0000)
43  | (static_cast<int32>(value[1]) << 8 & 0x0000FF00) | (static_cast<int32>(value[0]) & 0x000000FF);
44 #endif
45 }
46 
50 CPP_UTILITIES_EXPORT constexpr uint32 toUInt24(const char *value)
51 {
52 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
53  return (static_cast<uint32>(value[0]) << 16 & 0x00FF0000) | (static_cast<uint32>(value[1]) << 8 & 0x0000FF00)
54  | (static_cast<uint32>(value[2]) & 0x000000FF);
55 #else
56  return (static_cast<uint32>(value[2]) << 16 & 0x00FF0000) | (static_cast<uint32>(value[1]) << 8 & 0x0000FF00)
57  | (static_cast<uint32>(value[0]) & 0x000000FF);
58 #endif
59 }
60 
64 CPP_UTILITIES_EXPORT constexpr uint32 toUInt32(const char *value)
65 {
66 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
67  return (static_cast<uint32>(value[0]) << 24 & 0xFF000000) | (static_cast<uint32>(value[1]) << 16 & 0x00FF0000)
68  | (static_cast<uint32>(value[2]) << 8 & 0x0000FF00) | (static_cast<uint32>(value[3]) & 0x000000FF);
69 #else
70  return (static_cast<uint32>(value[3]) << 24 & 0xFF000000) | (static_cast<uint32>(value[2]) << 16 & 0x00FF0000)
71  | (static_cast<uint32>(value[1]) << 8 & 0x0000FF00) | (static_cast<uint32>(value[0]) & 0x000000FF);
72 #endif
73 }
74 
78 CPP_UTILITIES_EXPORT constexpr int64 toInt64(const char *value)
79 {
80 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
81  return (static_cast<int64>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<int64>(value[1]) << 48 & 0x00FF000000000000)
82  | (static_cast<int64>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<int64>(value[3]) << 32 & 0x000000FF00000000)
83  | (static_cast<int64>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<int64>(value[5]) << 16 & 0x0000000000FF0000)
84  | (static_cast<int64>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<int64>(value[7]) & 0x00000000000000FF);
85 #else
86  return (static_cast<int64>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<int64>(value[6]) << 48 & 0x00FF000000000000)
87  | (static_cast<int64>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<int64>(value[4]) << 32 & 0x000000FF00000000)
88  | (static_cast<int64>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<int64>(value[2]) << 16 & 0x0000000000FF0000)
89  | (static_cast<int64>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<int64>(value[0]) & 0x00000000000000FF);
90 #endif
91 }
92 
96 CPP_UTILITIES_EXPORT constexpr uint64 toUInt64(const char *value)
97 {
98 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
99  return (static_cast<uint64>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<uint64>(value[1]) << 48 & 0x00FF000000000000)
100  | (static_cast<uint64>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<uint64>(value[3]) << 32 & 0x000000FF00000000)
101  | (static_cast<uint64>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<uint64>(value[5]) << 16 & 0x0000000000FF0000)
102  | (static_cast<uint64>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<uint64>(value[7]) & 0x00000000000000FF);
103 #else
104  return (static_cast<uint64>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<uint64>(value[6]) << 48 & 0x00FF000000000000)
105  | (static_cast<uint64>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<uint64>(value[4]) << 32 & 0x000000FF00000000)
106  | (static_cast<uint64>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<uint64>(value[2]) << 16 & 0x0000000000FF0000)
107  | (static_cast<uint64>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<uint64>(value[0]) & 0x00000000000000FF);
108 #endif
109 }
110 
114 CPP_UTILITIES_EXPORT inline float32 toFloat32(const char *value)
115 {
116  const int32 val = toInt32(value);
117  const char *const c = reinterpret_cast<const char *>(&val);
118  return *reinterpret_cast<const float32 *>(c);
119 }
120 
124 CPP_UTILITIES_EXPORT inline float64 toFloat64(const char *value)
125 {
126  const int64 val = toInt64(value);
127  const char *const c = reinterpret_cast<const char *>(&val);
128  return *reinterpret_cast<const float64 *const>(c);
129 }
130 
134 CPP_UTILITIES_EXPORT inline void getBytes(int16 value, char *outputbuffer)
135 {
136 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
137  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
138  outputbuffer[1] = static_cast<char>((value)&0xFF);
139 #else
140  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
141  outputbuffer[0] = static_cast<char>((value)&0xFF);
142 #endif
143 }
144 
148 CPP_UTILITIES_EXPORT inline void getBytes(uint16 value, char *outputbuffer)
149 {
150 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
151  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
152  outputbuffer[1] = static_cast<char>((value)&0xFF);
153 #else
154  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
155  outputbuffer[0] = static_cast<char>((value)&0xFF);
156 #endif
157 }
158 
163 CPP_UTILITIES_EXPORT inline void getBytes24(uint32 value, char *outputbuffer)
164 {
165 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
166  outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
167  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
168  outputbuffer[2] = static_cast<char>((value)&0xFF);
169 #else
170  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
171  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
172  outputbuffer[0] = static_cast<char>((value)&0xFF);
173 #endif
174 }
175 
179 CPP_UTILITIES_EXPORT inline void getBytes(int32 value, char *outputbuffer)
180 {
181 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
182  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
183  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
184  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
185  outputbuffer[3] = static_cast<char>((value)&0xFF);
186 #else
187  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
188  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
189  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
190  outputbuffer[0] = static_cast<char>((value)&0xFF);
191 #endif
192 }
193 
197 CPP_UTILITIES_EXPORT inline void getBytes(uint32 value, char *outputbuffer)
198 {
199 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
200  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
201  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
202  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
203  outputbuffer[3] = static_cast<char>((value)&0xFF);
204 #else
205  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
206  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
207  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
208  outputbuffer[0] = static_cast<char>((value)&0xFF);
209 #endif
210 }
211 
215 CPP_UTILITIES_EXPORT inline void getBytes(int64 value, char *outputbuffer)
216 {
217 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
218  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
219  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
220  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
221  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
222  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
223  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
224  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
225  outputbuffer[7] = static_cast<char>((value)&0xFF);
226 #else
227  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
228  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
229  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
230  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
231  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
232  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
233  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
234  outputbuffer[0] = static_cast<char>((value)&0xFF);
235 #endif
236 }
237 
241 CPP_UTILITIES_EXPORT inline void getBytes(uint64 value, char *outputbuffer)
242 {
243 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
244  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
245  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
246  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
247  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
248  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
249  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
250  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
251  outputbuffer[7] = static_cast<char>((value)&0xFF);
252 #else
253  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
254  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
255  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
256  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
257  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
258  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
259  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
260  outputbuffer[0] = static_cast<char>((value)&0xFF);
261 #endif
262 }
263 
267 CPP_UTILITIES_EXPORT inline void getBytes(float32 value, char *outputbuffer)
268 {
269  char *c = reinterpret_cast<char *>(&value);
270  int32 i = *reinterpret_cast<int32 *>(c);
271  getBytes(i, outputbuffer);
272 }
273 
277 CPP_UTILITIES_EXPORT inline void getBytes(float64 value, char *outputbuffer)
278 {
279  char *c = reinterpret_cast<char *>(&value);
280  int64 i = *reinterpret_cast<int64 *>(c);
281  getBytes(i, outputbuffer);
282 }
283 
284 #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
CPP_UTILITIES_EXPORT constexpr uint16 toUInt16(const char *value)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr uint32 toUInt24(const char *value)
Returns a 32-bit unsigned integer converted from three bytes at a specified position in a char array.
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
#define CPP_UTILITIES_EXPORT
CPP_UTILITIES_EXPORT constexpr int32 toInt32(const char *value)
Returns a 32-bit signed integer converted from four bytes at a specified position in a char array.
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
CPP_UTILITIES_EXPORT constexpr int16 toInt16(const char *value)
Returns a 16-bit signed integer converted from two bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT float32 toFloat32(const char *value)
Returns a 32-bit floating point number converted from four bytes at a specified position in a char ar...
constexpr int i
CPP_UTILITIES_EXPORT void getBytes24(uint32 value, char *outputbuffer)
Stores the specified 24-bit unsigned integer value at a specified position in a char array.
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
CPP_UTILITIES_EXPORT float64 toFloat64(const char *value)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
CPP_UTILITIES_EXPORT constexpr uint64 toUInt64(const char *value)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr int64 toInt64(const char *value)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a char array.
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39
CPP_UTILITIES_EXPORT void getBytes(int16 value, char *outputbuffer)
Stores the specified 16-bit signed integer value at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr uint32 toUInt32(const char *value)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a char array.