C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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)
16  | (static_cast<int16>(value[1]) & 0x00FF);
17 #else
18  return (static_cast<int16>(value[1]) << 8 & 0xFF00)
19  | (static_cast<int16>(value[0]) & 0x00FF);
20 #endif
21 }
22 
26 CPP_UTILITIES_EXPORT inline uint16 toUInt16(const char *value)
27 {
28 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
29  return (static_cast<uint16>(value[0]) << 8 & 0xFF00)
30  | (static_cast<uint16>(value[1]) & 0x00FF);
31 #else
32  return (static_cast<uint16>(value[1]) << 8 & 0xFF00)
33  | (static_cast<uint16>(value[0]) & 0x00FF);
34 #endif
35 }
36 
40 CPP_UTILITIES_EXPORT inline int32 toInt32(const char *value)
41 {
42 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
43  return (static_cast<int32>(value[0]) << 24 & 0xFF000000)
44  | (static_cast<int32>(value[1]) << 16 & 0x00FF0000)
45  | (static_cast<int32>(value[2]) << 8 & 0x0000FF00)
46  | (static_cast<int32>(value[3]) & 0x000000FF);
47 #else
48  return (static_cast<int32>(value[3]) << 24 & 0xFF000000)
49  | (static_cast<int32>(value[2]) << 16 & 0x00FF0000)
50  | (static_cast<int32>(value[1]) << 8 & 0x0000FF00)
51  | (static_cast<int32>(value[0]) & 0x000000FF);
52 #endif
53 }
54 
58 CPP_UTILITIES_EXPORT inline uint32 toUInt24(const char *value)
59 {
60 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
61  return (static_cast<uint32>(value[0]) << 16 & 0x00FF0000)
62  | (static_cast<uint32>(value[1]) << 8 & 0x0000FF00)
63  | (static_cast<uint32>(value[2]) & 0x000000FF);
64 #else
65  return (static_cast<uint32>(value[2]) << 16 & 0x00FF0000)
66  | (static_cast<uint32>(value[1]) << 8 & 0x0000FF00)
67  | (static_cast<uint32>(value[0]) & 0x000000FF);
68 #endif
69 }
70 
74 CPP_UTILITIES_EXPORT inline uint32 toUInt32(const char *value)
75 {
76 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
77  return (static_cast<uint32>(value[0]) << 24 & 0xFF000000)
78  | (static_cast<uint32>(value[1]) << 16 & 0x00FF0000)
79  | (static_cast<uint32>(value[2]) << 8 & 0x0000FF00)
80  | (static_cast<uint32>(value[3]) & 0x000000FF);
81 #else
82  return (static_cast<uint32>(value[3]) << 24 & 0xFF000000)
83  | (static_cast<uint32>(value[2]) << 16 & 0x00FF0000)
84  | (static_cast<uint32>(value[1]) << 8 & 0x0000FF00)
85  | (static_cast<uint32>(value[0]) & 0x000000FF);
86 #endif
87 }
88 
92 CPP_UTILITIES_EXPORT inline int64 toInt64(const char *value)
93 {
94 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
95  return (static_cast<int64>(value[0]) << 56 & 0xFF00000000000000)
96  | (static_cast<int64>(value[1]) << 48 & 0x00FF000000000000)
97  | (static_cast<int64>(value[2]) << 40 & 0x0000FF0000000000)
98  | (static_cast<int64>(value[3]) << 32 & 0x000000FF00000000)
99  | (static_cast<int64>(value[4]) << 24 & 0x00000000FF000000)
100  | (static_cast<int64>(value[5]) << 16 & 0x0000000000FF0000)
101  | (static_cast<int64>(value[6]) << 8 & 0x000000000000FF00)
102  | (static_cast<int64>(value[7]) & 0x00000000000000FF);
103 #else
104  return (static_cast<int64>(value[7]) << 56 & 0xFF00000000000000)
105  | (static_cast<int64>(value[6]) << 48 & 0x00FF000000000000)
106  | (static_cast<int64>(value[5]) << 40 & 0x0000FF0000000000)
107  | (static_cast<int64>(value[4]) << 32 & 0x000000FF00000000)
108  | (static_cast<int64>(value[3]) << 24 & 0x00000000FF000000)
109  | (static_cast<int64>(value[2]) << 16 & 0x0000000000FF0000)
110  | (static_cast<int64>(value[1]) << 8 & 0x000000000000FF00)
111  | (static_cast<int64>(value[0]) & 0x00000000000000FF);
112 #endif
113 }
114 
118 CPP_UTILITIES_EXPORT inline uint64 toUInt64(const char *value)
119 {
120 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
121  return (static_cast<uint64>(value[0]) << 56 & 0xFF00000000000000)
122  | (static_cast<uint64>(value[1]) << 48 & 0x00FF000000000000)
123  | (static_cast<uint64>(value[2]) << 40 & 0x0000FF0000000000)
124  | (static_cast<uint64>(value[3]) << 32 & 0x000000FF00000000)
125  | (static_cast<uint64>(value[4]) << 24 & 0x00000000FF000000)
126  | (static_cast<uint64>(value[5]) << 16 & 0x0000000000FF0000)
127  | (static_cast<uint64>(value[6]) << 8 & 0x000000000000FF00)
128  | (static_cast<uint64>(value[7]) & 0x00000000000000FF);
129 #else
130  return (static_cast<uint64>(value[7]) << 56 & 0xFF00000000000000)
131  | (static_cast<uint64>(value[6]) << 48 & 0x00FF000000000000)
132  | (static_cast<uint64>(value[5]) << 40 & 0x0000FF0000000000)
133  | (static_cast<uint64>(value[4]) << 32 & 0x000000FF00000000)
134  | (static_cast<uint64>(value[3]) << 24 & 0x00000000FF000000)
135  | (static_cast<uint64>(value[2]) << 16 & 0x0000000000FF0000)
136  | (static_cast<uint64>(value[1]) << 8 & 0x000000000000FF00)
137  | (static_cast<uint64>(value[0]) & 0x00000000000000FF);
138 #endif
139 }
140 
144 CPP_UTILITIES_EXPORT inline float32 toFloat32(const char *value)
145 {
146 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
147  int32 val = toInt32(value);
148  char *c = reinterpret_cast<char *>(&val);
149  return *reinterpret_cast<float32 *>(c);
150 #else
151  int32 val = toInt32(value);
152  char *c = reinterpret_cast<char *>(&val);
153  return *reinterpret_cast<float32 *>(c);
154 #endif
155 }
156 
160 CPP_UTILITIES_EXPORT inline float64 toFloat64(const char *value)
161 {
162 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
163  int64 val = toInt64(value);
164  char *c = reinterpret_cast<char *>(&val);
165  return *reinterpret_cast<float64*>(c);
166 #else
167  int64 val = toInt64(value);
168  char *c = reinterpret_cast<char *>(&val);
169  return *reinterpret_cast<float64 *>(c);
170 #endif
171 }
172 
176 CPP_UTILITIES_EXPORT inline void getBytes(int16 value, char *outputbuffer)
177 {
178 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
179  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
180  outputbuffer[1] = static_cast<char>((value ) & 0xFF);
181 #else
182  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
183  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
184 #endif
185 }
186 
190 CPP_UTILITIES_EXPORT inline void getBytes(uint16 value, char *outputbuffer)
191 {
192 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
193  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
194  outputbuffer[1] = static_cast<char>((value ) & 0xFF);
195 #else
196  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
197  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
198 #endif
199 }
200 
205 CPP_UTILITIES_EXPORT inline void getBytes24(uint32 value, char *outputbuffer)
206 {
207 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
208  outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
209  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
210  outputbuffer[2] = static_cast<char>((value ) & 0xFF);
211 #else
212  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
213  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
214  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
215 #endif
216 }
217 
221 CPP_UTILITIES_EXPORT inline void getBytes(int32 value, char *outputbuffer)
222 {
223 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
224  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
225  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
226  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
227  outputbuffer[3] = static_cast<char>((value ) & 0xFF);
228 #else
229  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
230  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
231  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
232  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
233 #endif
234 }
235 
239 CPP_UTILITIES_EXPORT inline void getBytes(uint32 value, char *outputbuffer)
240 {
241 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
242  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
243  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
244  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
245  outputbuffer[3] = static_cast<char>((value ) & 0xFF);
246 #else
247  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
248  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
249  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
250  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
251 #endif
252 }
253 
257 CPP_UTILITIES_EXPORT inline void getBytes(int64 value, char *outputbuffer)
258 {
259 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
260  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
261  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
262  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
263  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
264  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
265  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
266  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
267  outputbuffer[7] = static_cast<char>((value ) & 0xFF);
268 #else
269  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
270  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
271  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
272  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
273  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
274  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
275  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
276  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
277 #endif
278 }
279 
283 CPP_UTILITIES_EXPORT inline void getBytes(uint64 value, char *outputbuffer)
284 {
285 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
286  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
287  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
288  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
289  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
290  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
291  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
292  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
293  outputbuffer[7] = static_cast<char>((value ) & 0xFF);
294 #else
295  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
296  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
297  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
298  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
299  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
300  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
301  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
302  outputbuffer[0] = static_cast<char>((value ) & 0xFF);
303 #endif
304 }
305 
309 CPP_UTILITIES_EXPORT inline void getBytes(float32 value, char *outputbuffer)
310 {
311  char *c = reinterpret_cast<char *>(&value);
312  int32 i = *reinterpret_cast<int32 *>(c);
313  getBytes(i, outputbuffer);
314 }
315 
319 CPP_UTILITIES_EXPORT inline void getBytes(float64 value, char *outputbuffer)
320 {
321  char *c = reinterpret_cast<char *>(&value);
322  int64 i = *reinterpret_cast<int64 *>(c);
323  getBytes(i, outputbuffer);
324 }
325 
326 #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