C++ Utilities  5.10.5
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 "../global.h"
6 
7 #include <cstdint>
8 
9 // disable warnings about sign conversions when using GCC or Clang
10 #ifdef __GNUC__
11 #pragma GCC diagnostic push
12 #pragma GCC diagnostic ignored "-Wsign-conversion"
13 #endif
14 
18 CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value)
19 {
20 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
21  return static_cast<std::int16_t>((static_cast<std::int16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[1]) & 0x00FF));
22 #else
23  return static_cast<std::int16_t>((static_cast<std::int16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[0]) & 0x00FF));
24 #endif
25 }
26 
30 CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value)
31 {
32 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
33  return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[1]) & 0x00FF));
34 #else
35  return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[0]) & 0x00FF));
36 #endif
37 }
38 
42 CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value)
43 {
44 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
45  return static_cast<std::int32_t>((static_cast<std::int32_t>(value[0]) << 24 & 0xFF000000)
46  | (static_cast<std::int32_t>(value[1]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[2]) << 8 & 0x0000FF00)
47  | (static_cast<std::int32_t>(value[3]) & 0x000000FF));
48 #else
49  return static_cast<std::int32_t>((static_cast<std::int32_t>(value[3]) << 24 & 0xFF000000)
50  | (static_cast<std::int32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[1]) << 8 & 0x0000FF00)
51  | (static_cast<std::int32_t>(value[0]) & 0x000000FF));
52 #endif
53 }
54 
58 CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt24(const char *value)
59 {
60 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
61  return (static_cast<std::uint32_t>(value[0]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
62  | (static_cast<std::uint32_t>(value[2]) & 0x000000FF);
63 #else
64  return (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
65  | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
66 #endif
67 }
68 
72 CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt32(const char *value)
73 {
74 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
75  return (static_cast<std::uint32_t>(value[0]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[1]) << 16 & 0x00FF0000)
76  | (static_cast<std::uint32_t>(value[2]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[3]) & 0x000000FF);
77 #else
78  return (static_cast<std::uint32_t>(value[3]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000)
79  | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
80 #endif
81 }
82 
86 CPP_UTILITIES_EXPORT constexpr std::int64_t toInt64(const char *value)
87 {
88 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
89  return (static_cast<std::int64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[1]) << 48 & 0x00FF000000000000)
90  | (static_cast<std::int64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[3]) << 32 & 0x000000FF00000000)
91  | (static_cast<std::int64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[5]) << 16 & 0x0000000000FF0000)
92  | (static_cast<std::int64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[7]) & 0x00000000000000FF);
93 #else
94  return (static_cast<std::int64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[6]) << 48 & 0x00FF000000000000)
95  | (static_cast<std::int64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[4]) << 32 & 0x000000FF00000000)
96  | (static_cast<std::int64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[2]) << 16 & 0x0000000000FF0000)
97  | (static_cast<std::int64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[0]) & 0x00000000000000FF);
98 #endif
99 }
100 
104 CPP_UTILITIES_EXPORT constexpr std::uint64_t toUInt64(const char *value)
105 {
106 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
107  return (static_cast<std::uint64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[1]) << 48 & 0x00FF000000000000)
108  | (static_cast<std::uint64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[3]) << 32 & 0x000000FF00000000)
109  | (static_cast<std::uint64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[5]) << 16 & 0x0000000000FF0000)
110  | (static_cast<std::uint64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[7]) & 0x00000000000000FF);
111 #else
112  return (static_cast<std::uint64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[6]) << 48 & 0x00FF000000000000)
113  | (static_cast<std::uint64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[4]) << 32 & 0x000000FF00000000)
114  | (static_cast<std::uint64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[2]) << 16 & 0x0000000000FF0000)
115  | (static_cast<std::uint64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[0]) & 0x00000000000000FF);
116 #endif
117 }
118 
122 CPP_UTILITIES_EXPORT inline float toFloat32(const char *value)
123 {
124  const auto val = toInt32(value);
125  const auto *const c = reinterpret_cast<const char *>(&val);
126  return *reinterpret_cast<const float *>(c);
127 }
128 
132 CPP_UTILITIES_EXPORT inline double toFloat64(const char *value)
133 {
134  const auto val = toInt64(value);
135  const auto *const c = reinterpret_cast<const char *>(&val);
136  return *reinterpret_cast<const double *>(c);
137 }
138 
142 CPP_UTILITIES_EXPORT inline void getBytes(std::int16_t value, char *outputbuffer)
143 {
144 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
145  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
146  outputbuffer[1] = static_cast<char>((value)&0xFF);
147 #else
148  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
149  outputbuffer[0] = static_cast<char>((value)&0xFF);
150 #endif
151 }
152 
156 CPP_UTILITIES_EXPORT inline void getBytes(std::uint16_t value, char *outputbuffer)
157 {
158 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
159  outputbuffer[0] = static_cast<char>((value >> 8) & 0xFF);
160  outputbuffer[1] = static_cast<char>((value)&0xFF);
161 #else
162  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
163  outputbuffer[0] = static_cast<char>((value)&0xFF);
164 #endif
165 }
166 
171 CPP_UTILITIES_EXPORT inline void getBytes24(std::uint32_t value, char *outputbuffer)
172 {
173 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
174  outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
175  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
176  outputbuffer[2] = static_cast<char>((value)&0xFF);
177 #else
178  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
179  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
180  outputbuffer[0] = static_cast<char>((value)&0xFF);
181 #endif
182 }
183 
187 CPP_UTILITIES_EXPORT inline void getBytes(std::int32_t value, char *outputbuffer)
188 {
189 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
190  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
191  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
192  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
193  outputbuffer[3] = static_cast<char>((value)&0xFF);
194 #else
195  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
196  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
197  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
198  outputbuffer[0] = static_cast<char>((value)&0xFF);
199 #endif
200 }
201 
205 CPP_UTILITIES_EXPORT inline void getBytes(std::uint32_t value, char *outputbuffer)
206 {
207 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
208  outputbuffer[0] = static_cast<char>((value >> 24) & 0xFF);
209  outputbuffer[1] = static_cast<char>((value >> 16) & 0xFF);
210  outputbuffer[2] = static_cast<char>((value >> 8) & 0xFF);
211  outputbuffer[3] = static_cast<char>((value)&0xFF);
212 #else
213  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
214  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
215  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
216  outputbuffer[0] = static_cast<char>((value)&0xFF);
217 #endif
218 }
219 
223 CPP_UTILITIES_EXPORT inline void getBytes(std::int64_t value, char *outputbuffer)
224 {
225 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
226  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
227  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
228  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
229  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
230  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
231  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
232  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
233  outputbuffer[7] = static_cast<char>((value)&0xFF);
234 #else
235  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
236  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
237  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
238  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
239  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
240  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
241  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
242  outputbuffer[0] = static_cast<char>((value)&0xFF);
243 #endif
244 }
245 
249 CPP_UTILITIES_EXPORT inline void getBytes(std::uint64_t value, char *outputbuffer)
250 {
251 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
252  outputbuffer[0] = static_cast<char>((value >> 56) & 0xFF);
253  outputbuffer[1] = static_cast<char>((value >> 48) & 0xFF);
254  outputbuffer[2] = static_cast<char>((value >> 40) & 0xFF);
255  outputbuffer[3] = static_cast<char>((value >> 32) & 0xFF);
256  outputbuffer[4] = static_cast<char>((value >> 24) & 0xFF);
257  outputbuffer[5] = static_cast<char>((value >> 16) & 0xFF);
258  outputbuffer[6] = static_cast<char>((value >> 8) & 0xFF);
259  outputbuffer[7] = static_cast<char>((value)&0xFF);
260 #else
261  outputbuffer[7] = static_cast<char>((value >> 56) & 0xFF);
262  outputbuffer[6] = static_cast<char>((value >> 48) & 0xFF);
263  outputbuffer[5] = static_cast<char>((value >> 40) & 0xFF);
264  outputbuffer[4] = static_cast<char>((value >> 32) & 0xFF);
265  outputbuffer[3] = static_cast<char>((value >> 24) & 0xFF);
266  outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
267  outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
268  outputbuffer[0] = static_cast<char>((value)&0xFF);
269 #endif
270 }
271 
275 CPP_UTILITIES_EXPORT inline void getBytes(float value, char *outputbuffer)
276 {
277  auto *c = reinterpret_cast<char *>(&value);
278  auto i = *reinterpret_cast<std::int32_t *>(c);
279  getBytes(i, outputbuffer);
280 }
281 
285 CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer)
286 {
287  auto *c = reinterpret_cast<char *>(&value);
288  auto i = *reinterpret_cast<std::int64_t *>(c);
289  getBytes(i, outputbuffer);
290 }
291 
292 #ifdef __GNUC__
293 #pragma GCC diagnostic pop
294 #endif
295 
296 #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
constexpr CPP_UTILITIES_EXPORT float toFloat32(std::uint16_t fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.
constexpr int i