C++ Utilities  5.5.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 "../global.h"
6 
7 #include <cstdint>
8 
12 CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value)
13 {
14 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
15  return (static_cast<std::int16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[1]) & 0x00FF);
16 #else
17  return (static_cast<std::int16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[0]) & 0x00FF);
18 #endif
19 }
20 
24 CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value)
25 {
26 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
27  return (static_cast<std::uint16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[1]) & 0x00FF);
28 #else
29  return (static_cast<std::uint16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[0]) & 0x00FF);
30 #endif
31 }
32 
36 CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value)
37 {
38 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
39  return (static_cast<std::int32_t>(value[0]) << 24 & 0xFF000000) | (static_cast<std::int32_t>(value[1]) << 16 & 0x00FF0000)
40  | (static_cast<std::int32_t>(value[2]) << 8 & 0x0000FF00) | (static_cast<std::int32_t>(value[3]) & 0x000000FF);
41 #else
42  return (static_cast<std::int32_t>(value[3]) << 24 & 0xFF000000) | (static_cast<std::int32_t>(value[2]) << 16 & 0x00FF0000)
43  | (static_cast<std::int32_t>(value[1]) << 8 & 0x0000FF00) | (static_cast<std::int32_t>(value[0]) & 0x000000FF);
44 #endif
45 }
46 
50 CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt24(const char *value)
51 {
52 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
53  return (static_cast<std::uint32_t>(value[0]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
54  | (static_cast<std::uint32_t>(value[2]) & 0x000000FF);
55 #else
56  return (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
57  | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
58 #endif
59 }
60 
64 CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt32(const char *value)
65 {
66 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
67  return (static_cast<std::uint32_t>(value[0]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[1]) << 16 & 0x00FF0000)
68  | (static_cast<std::uint32_t>(value[2]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[3]) & 0x000000FF);
69 #else
70  return (static_cast<std::uint32_t>(value[3]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000)
71  | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
72 #endif
73 }
74 
78 CPP_UTILITIES_EXPORT constexpr std::int64_t toInt64(const char *value)
79 {
80 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
81  return (static_cast<std::int64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[1]) << 48 & 0x00FF000000000000)
82  | (static_cast<std::int64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[3]) << 32 & 0x000000FF00000000)
83  | (static_cast<std::int64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[5]) << 16 & 0x0000000000FF0000)
84  | (static_cast<std::int64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[7]) & 0x00000000000000FF);
85 #else
86  return (static_cast<std::int64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[6]) << 48 & 0x00FF000000000000)
87  | (static_cast<std::int64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[4]) << 32 & 0x000000FF00000000)
88  | (static_cast<std::int64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[2]) << 16 & 0x0000000000FF0000)
89  | (static_cast<std::int64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[0]) & 0x00000000000000FF);
90 #endif
91 }
92 
96 CPP_UTILITIES_EXPORT constexpr std::uint64_t toUInt64(const char *value)
97 {
98 #if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
99  return (static_cast<std::uint64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[1]) << 48 & 0x00FF000000000000)
100  | (static_cast<std::uint64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[3]) << 32 & 0x000000FF00000000)
101  | (static_cast<std::uint64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[5]) << 16 & 0x0000000000FF0000)
102  | (static_cast<std::uint64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[7]) & 0x00000000000000FF);
103 #else
104  return (static_cast<std::uint64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[6]) << 48 & 0x00FF000000000000)
105  | (static_cast<std::uint64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[4]) << 32 & 0x000000FF00000000)
106  | (static_cast<std::uint64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[2]) << 16 & 0x0000000000FF0000)
107  | (static_cast<std::uint64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[0]) & 0x00000000000000FF);
108 #endif
109 }
110 
114 CPP_UTILITIES_EXPORT inline float toFloat32(const char *value)
115 {
116  const auto val = toInt32(value);
117  const auto *const c = reinterpret_cast<const char *>(&val);
118  return *reinterpret_cast<const float *>(c);
119 }
120 
124 CPP_UTILITIES_EXPORT inline double toFloat64(const char *value)
125 {
126  const auto val = toInt64(value);
127  const auto *const c = reinterpret_cast<const char *>(&val);
128  return *reinterpret_cast<const double *const>(c);
129 }
130 
134 CPP_UTILITIES_EXPORT inline void getBytes(std::int16_t 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(std::uint16_t 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(std::uint32_t 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(std::int32_t 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(std::uint32_t 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(std::int64_t 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(std::uint64_t 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(float value, char *outputbuffer)
268 {
269  auto *c = reinterpret_cast<char *>(&value);
270  auto i = *reinterpret_cast<std::int32_t *>(c);
271  getBytes(i, outputbuffer);
272 }
273 
277 CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer)
278 {
279  auto *c = reinterpret_cast<char *>(&value);
280  auto i = *reinterpret_cast<std::int64_t *>(c);
281  getBytes(i, outputbuffer);
282 }
283 
284 #endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
i
constexpr int i
Definition: traitstests.cpp:106
CppUtilities::toFloat32
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.
Definition: binaryconversion.h:109
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.