C++ Utilities 5.22.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
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// disable warnings about sign conversions when using GCC or Clang
6#ifdef __GNUC__
7#pragma GCC diagnostic push
8#pragma GCC diagnostic ignored "-Wsign-conversion"
9#endif
10
14CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value)
15{
16#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
17 return static_cast<std::int16_t>((static_cast<std::int16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[1]) & 0x00FF));
18#else
19 return static_cast<std::int16_t>((static_cast<std::int16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[0]) & 0x00FF));
20#endif
21}
22
26CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value)
27{
28#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
29 return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[1]) & 0x00FF));
30#else
31 return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[0]) & 0x00FF));
32#endif
33}
34
38CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value)
39{
40#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
41 return static_cast<std::int32_t>((static_cast<std::int32_t>(value[0]) << 24 & 0xFF000000)
42 | (static_cast<std::int32_t>(value[1]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[2]) << 8 & 0x0000FF00)
43 | (static_cast<std::int32_t>(value[3]) & 0x000000FF));
44#else
45 return static_cast<std::int32_t>((static_cast<std::int32_t>(value[3]) << 24 & 0xFF000000)
46 | (static_cast<std::int32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[1]) << 8 & 0x0000FF00)
47 | (static_cast<std::int32_t>(value[0]) & 0x000000FF));
48#endif
49}
50
54CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt24(const char *value)
55{
56#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
57 return (static_cast<std::uint32_t>(value[0]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
58 | (static_cast<std::uint32_t>(value[2]) & 0x000000FF);
59#else
60 return (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
61 | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
62#endif
63}
64
68CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt32(const char *value)
69{
70#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
71 return (static_cast<std::uint32_t>(value[0]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[1]) << 16 & 0x00FF0000)
72 | (static_cast<std::uint32_t>(value[2]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[3]) & 0x000000FF);
73#else
74 return (static_cast<std::uint32_t>(value[3]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000)
75 | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
76#endif
77}
78
82CPP_UTILITIES_EXPORT constexpr std::int64_t toInt64(const char *value)
83{
84#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
85 return (static_cast<std::int64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[1]) << 48 & 0x00FF000000000000)
86 | (static_cast<std::int64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[3]) << 32 & 0x000000FF00000000)
87 | (static_cast<std::int64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[5]) << 16 & 0x0000000000FF0000)
88 | (static_cast<std::int64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[7]) & 0x00000000000000FF);
89#else
90 return (static_cast<std::int64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[6]) << 48 & 0x00FF000000000000)
91 | (static_cast<std::int64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[4]) << 32 & 0x000000FF00000000)
92 | (static_cast<std::int64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[2]) << 16 & 0x0000000000FF0000)
93 | (static_cast<std::int64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[0]) & 0x00000000000000FF);
94#endif
95}
96
100CPP_UTILITIES_EXPORT constexpr std::uint64_t toUInt64(const char *value)
101{
102#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
103 return (static_cast<std::uint64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[1]) << 48 & 0x00FF000000000000)
104 | (static_cast<std::uint64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[3]) << 32 & 0x000000FF00000000)
105 | (static_cast<std::uint64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[5]) << 16 & 0x0000000000FF0000)
106 | (static_cast<std::uint64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[7]) & 0x00000000000000FF);
107#else
108 return (static_cast<std::uint64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[6]) << 48 & 0x00FF000000000000)
109 | (static_cast<std::uint64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[4]) << 32 & 0x000000FF00000000)
110 | (static_cast<std::uint64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[2]) << 16 & 0x0000000000FF0000)
111 | (static_cast<std::uint64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[0]) & 0x00000000000000FF);
112#endif
113}
114
118CPP_UTILITIES_EXPORT inline float toFloat32(const char *value)
119{
120 const auto val = toInt32(value);
121 const auto *const c = reinterpret_cast<const char *>(&val);
122 return *reinterpret_cast<const float *>(c);
123}
124
128CPP_UTILITIES_EXPORT inline double toFloat64(const char *value)
129{
130 const auto val = toInt64(value);
131 const auto *const c = reinterpret_cast<const char *>(&val);
132 return *reinterpret_cast<const double *>(c);
133}
134
143template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline T toInt(const char *value)
144{
145 auto dst = T();
146 std::memcpy(&dst, value, sizeof(T));
147#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
148 dst = swapOrder(dst);
149#endif
150 return dst;
151}
152
157CPP_UTILITIES_EXPORT inline void getBytes24(std::uint32_t value, char *outputbuffer)
158{
159#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
160 outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
161 outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
162 outputbuffer[2] = static_cast<char>((value)&0xFF);
163#else
164 outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
165 outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
166 outputbuffer[0] = static_cast<char>((value)&0xFF);
167#endif
168}
169
177template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline void getBytes(T value, char *outputbuffer)
178{
179#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
180 value = swapOrder(value);
181#endif
182 std::memcpy(outputbuffer, &value, sizeof(T));
183}
184
188CPP_UTILITIES_EXPORT inline void getBytes(float value, char *outputbuffer)
189{
190 auto *c = reinterpret_cast<char *>(&value);
191 auto i = *reinterpret_cast<std::int32_t *>(c);
192 getBytes(i, outputbuffer);
193}
194
198CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer)
199{
200 auto *c = reinterpret_cast<char *>(&value);
201 auto i = *reinterpret_cast<std::int64_t *>(c);
202 getBytes(i, outputbuffer);
203}
204
205#ifdef __GNUC__
206#pragma GCC diagnostic pop
207#endif
208
209#endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CPP_UTILITIES_EXPORT constexpr float toFloat32(std::uint16_t fixed8value)
Returns a 32-bit floating point number converted from the specified 8.8 fixed point representation.
CPP_UTILITIES_EXPORT constexpr std::uint16_t swapOrder(std::uint16_t value)
Swaps the byte order of the specified 16-bit unsigned integer.
constexpr int i