C++ Utilities  4.12.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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 inline 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 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
117  int32 val = toInt32(value);
118  char *c = reinterpret_cast<char *>(&val);
119  return *reinterpret_cast<float32 *>(c);
120 #else
121  int32 val = toInt32(value);
122  char *c = reinterpret_cast<char *>(&val);
123  return *reinterpret_cast<float32 *>(c);
124 #endif
125 }
126 
130 CPP_UTILITIES_EXPORT inline float64 toFloat64(const char *value)
131 {
132 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
133  int64 val = toInt64(value);
134  char *c = reinterpret_cast<char *>(&val);
135  return *reinterpret_cast<float64 *>(c);
136 #else
137  int64 val = toInt64(value);
138  char *c = reinterpret_cast<char *>(&val);
139  return *reinterpret_cast<float64 *>(c);
140 #endif
141 }
142 
146 CPP_UTILITIES_EXPORT inline void getBytes(int16 value, char *outputbuffer)
147 {
148 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
149  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
150  outputbuffer[1] = static_cast<char>((value)&0xFF);
151 #else
152  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
153  outputbuffer[0] = static_cast<char>((value)&0xFF);
154 #endif
155 }
156 
160 CPP_UTILITIES_EXPORT inline void getBytes(uint16 value, char *outputbuffer)
161 {
162 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
163  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
164  outputbuffer[1] = static_cast<char>((value)&0xFF);
165 #else
166  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
167  outputbuffer[0] = static_cast<char>((value)&0xFF);
168 #endif
169 }
170 
175 CPP_UTILITIES_EXPORT inline void getBytes24(uint32 value, char *outputbuffer)
176 {
177 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
178  outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
179  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
180  outputbuffer[2] = static_cast<char>((value)&0xFF);
181 #else
182  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
183  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
184  outputbuffer[0] = static_cast<char>((value)&0xFF);
185 #endif
186 }
187 
191 CPP_UTILITIES_EXPORT inline void getBytes(int32 value, char *outputbuffer)
192 {
193 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
194  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
195  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
196  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
197  outputbuffer[3] = static_cast<char>((value)&0xFF);
198 #else
199  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
200  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
201  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
202  outputbuffer[0] = static_cast<char>((value)&0xFF);
203 #endif
204 }
205 
209 CPP_UTILITIES_EXPORT inline void getBytes(uint32 value, char *outputbuffer)
210 {
211 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
212  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
213  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
214  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
215  outputbuffer[3] = static_cast<char>((value)&0xFF);
216 #else
217  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
218  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
219  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
220  outputbuffer[0] = static_cast<char>((value)&0xFF);
221 #endif
222 }
223 
227 CPP_UTILITIES_EXPORT inline void getBytes(int64 value, char *outputbuffer)
228 {
229 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
230  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
231  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
232  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
233  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
234  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
235  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
236  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
237  outputbuffer[7] = static_cast<char>((value)&0xFF);
238 #else
239  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
240  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
241  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
242  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
243  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
244  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
245  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
246  outputbuffer[0] = static_cast<char>((value)&0xFF);
247 #endif
248 }
249 
253 CPP_UTILITIES_EXPORT inline void getBytes(uint64 value, char *outputbuffer)
254 {
255 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
256  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
257  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
258  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
259  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
260  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
261  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
262  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
263  outputbuffer[7] = static_cast<char>((value)&0xFF);
264 #else
265  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
266  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
267  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
268  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
269  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
270  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
271  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
272  outputbuffer[0] = static_cast<char>((value)&0xFF);
273 #endif
274 }
275 
279 CPP_UTILITIES_EXPORT inline void getBytes(float32 value, char *outputbuffer)
280 {
281  char *c = reinterpret_cast<char *>(&value);
282  int32 i = *reinterpret_cast<int32 *>(c);
283  getBytes(i, outputbuffer);
284 }
285 
289 CPP_UTILITIES_EXPORT inline void getBytes(float64 value, char *outputbuffer)
290 {
291  char *c = reinterpret_cast<char *>(&value);
292  int64 i = *reinterpret_cast<int64 *>(c);
293  getBytes(i, outputbuffer);
294 }
295 
296 #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
std::uint32_t uint32
unsigned 32-bit integer
Definition: types.h:44
std::int32_t int32
signed 32-bit integer
Definition: types.h:24
CPP_UTILITIES_EXPORT constexpr float32 toFloat32(uint16 fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation...
std::int16_t int16
signed 16-bit integer
Definition: types.h:19
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
std::uint16_t uint16
unsigned 16-bit integer
Definition: types.h:39