C++ Utilities 5.37.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// assert that CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL is defined; a value of:
2// - 0 means to define functions for BE namespace
3// - 1 means to define functions for LE namespace
4#ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
5#error "Do not include binaryconversionprivate.h directly."
6#else
7
8// define macro if swapping byte order is required
9#if (CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0 && defined(CONVERSION_UTILITIES_BYTE_ORDER_LITTLE_ENDIAN)) \
10 || (CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 1 && defined(CONVERSION_UTILITIES_BYTE_ORDER_BIG_ENDIAN))
11#define CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
12#endif
13
14// disable warnings about sign conversions when using GCC or Clang
16CPP_UTILITIES_WARNING_DISABLE("-Wsign-conversion")
17
18
21CPP_UTILITIES_EXPORT constexpr std::int16_t toInt16(const char *value)
22{
23#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
24 return static_cast<std::int16_t>((static_cast<std::int16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[1]) & 0x00FF));
25#else
26 return static_cast<std::int16_t>((static_cast<std::int16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::int16_t>(value[0]) & 0x00FF));
27#endif
28}
29
33CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value)
34{
35#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
36 return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[0]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[1]) & 0x00FF));
37#else
38 return static_cast<std::uint16_t>((static_cast<std::uint16_t>(value[1]) << 8 & 0xFF00) | (static_cast<std::uint16_t>(value[0]) & 0x00FF));
39#endif
40}
41
45CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value)
46{
47#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
48 return static_cast<std::int32_t>((static_cast<std::int32_t>(value[0]) << 24 & 0xFF000000)
49 | (static_cast<std::int32_t>(value[1]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[2]) << 8 & 0x0000FF00)
50 | (static_cast<std::int32_t>(value[3]) & 0x000000FF));
51#else
52 return static_cast<std::int32_t>((static_cast<std::int32_t>(value[3]) << 24 & 0xFF000000)
53 | (static_cast<std::int32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::int32_t>(value[1]) << 8 & 0x0000FF00)
54 | (static_cast<std::int32_t>(value[0]) & 0x000000FF));
55#endif
56}
57
61CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt24(const char *value)
62{
63#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
64 return (static_cast<std::uint32_t>(value[0]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
65 | (static_cast<std::uint32_t>(value[2]) & 0x000000FF);
66#else
67 return (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000) | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00)
68 | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
69#endif
70}
71
75CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt32(const char *value)
76{
77#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
78 return (static_cast<std::uint32_t>(value[0]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[1]) << 16 & 0x00FF0000)
79 | (static_cast<std::uint32_t>(value[2]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[3]) & 0x000000FF);
80#else
81 return (static_cast<std::uint32_t>(value[3]) << 24 & 0xFF000000) | (static_cast<std::uint32_t>(value[2]) << 16 & 0x00FF0000)
82 | (static_cast<std::uint32_t>(value[1]) << 8 & 0x0000FF00) | (static_cast<std::uint32_t>(value[0]) & 0x000000FF);
83#endif
84}
85
89CPP_UTILITIES_EXPORT constexpr std::int64_t toInt64(const char *value)
90{
91#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
92 return (static_cast<std::int64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[1]) << 48 & 0x00FF000000000000)
93 | (static_cast<std::int64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[3]) << 32 & 0x000000FF00000000)
94 | (static_cast<std::int64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[5]) << 16 & 0x0000000000FF0000)
95 | (static_cast<std::int64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[7]) & 0x00000000000000FF);
96#else
97 return (static_cast<std::int64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::int64_t>(value[6]) << 48 & 0x00FF000000000000)
98 | (static_cast<std::int64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::int64_t>(value[4]) << 32 & 0x000000FF00000000)
99 | (static_cast<std::int64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::int64_t>(value[2]) << 16 & 0x0000000000FF0000)
100 | (static_cast<std::int64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::int64_t>(value[0]) & 0x00000000000000FF);
101#endif
102}
103
107CPP_UTILITIES_EXPORT constexpr std::uint64_t toUInt64(const char *value)
108{
109#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
110 return (static_cast<std::uint64_t>(value[0]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[1]) << 48 & 0x00FF000000000000)
111 | (static_cast<std::uint64_t>(value[2]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[3]) << 32 & 0x000000FF00000000)
112 | (static_cast<std::uint64_t>(value[4]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[5]) << 16 & 0x0000000000FF0000)
113 | (static_cast<std::uint64_t>(value[6]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[7]) & 0x00000000000000FF);
114#else
115 return (static_cast<std::uint64_t>(value[7]) << 56 & 0xFF00000000000000) | (static_cast<std::uint64_t>(value[6]) << 48 & 0x00FF000000000000)
116 | (static_cast<std::uint64_t>(value[5]) << 40 & 0x0000FF0000000000) | (static_cast<std::uint64_t>(value[4]) << 32 & 0x000000FF00000000)
117 | (static_cast<std::uint64_t>(value[3]) << 24 & 0x00000000FF000000) | (static_cast<std::uint64_t>(value[2]) << 16 & 0x0000000000FF0000)
118 | (static_cast<std::uint64_t>(value[1]) << 8 & 0x000000000000FF00) | (static_cast<std::uint64_t>(value[0]) & 0x00000000000000FF);
119#endif
120}
121
125CPP_UTILITIES_EXPORT inline float toFloat32(const char *value)
126{
127 const auto val = toInt32(value);
128 const auto *const c = reinterpret_cast<const char *>(&val);
129 return *reinterpret_cast<const float *>(c);
130}
131
135CPP_UTILITIES_EXPORT inline double toFloat64(const char *value)
136{
137 const auto val = toInt64(value);
138 const auto *const c = reinterpret_cast<const char *>(&val);
139 return *reinterpret_cast<const double *>(c);
140}
141
150template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline T toInt(const char *value)
151{
152 auto dst = T();
153 std::memcpy(&dst, value, sizeof(T));
154#ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
155 dst = swapOrder(dst);
156#endif
157 return dst;
158}
159
164CPP_UTILITIES_EXPORT inline void getBytes24(std::uint32_t value, char *outputbuffer)
165{
166#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
167 outputbuffer[0] = static_cast<char>((value >> 16) & 0xFF);
168 outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
169 outputbuffer[2] = static_cast<char>((value) & 0xFF);
170#else
171 outputbuffer[2] = static_cast<char>((value >> 16) & 0xFF);
172 outputbuffer[1] = static_cast<char>((value >> 8) & 0xFF);
173 outputbuffer[0] = static_cast<char>((value) & 0xFF);
174#endif
175}
176
184template <class T, Traits::EnableIf<std::is_integral<T>> * = nullptr> CPP_UTILITIES_EXPORT inline void getBytes(T value, char *outputbuffer)
185{
186#ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
187 value = swapOrder(value);
188#endif
189 std::memcpy(outputbuffer, &value, sizeof(T));
190}
191
195CPP_UTILITIES_EXPORT inline void getBytes(float value, char *outputbuffer)
196{
197 auto *c = reinterpret_cast<char *>(&value);
198 auto i = *reinterpret_cast<std::int32_t *>(c);
199 getBytes(i, outputbuffer);
200}
201
205CPP_UTILITIES_EXPORT inline void getBytes(double value, char *outputbuffer)
206{
207 auto *c = reinterpret_cast<char *>(&value);
208 auto i = *reinterpret_cast<std::int64_t *>(c);
209 getBytes(i, outputbuffer);
210}
211
213
214#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
215
216#endif // CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
#define CPP_UTILITIES_WARNING_PUSH
Causes GCC/Clang to remember the state of the diagnostics.
Definition global.h:154
#define CPP_UTILITIES_WARNING_POP
Causes GCC/Clang to restore the previous state of the diagnostics.
Definition global.h:155
#define CPP_UTILITIES_WARNING_DISABLE(text)
Causes GCC/Clang to suppress the specified diagnostic.
Definition global.h:156
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Definition global.h:14
CPP_UTILITIES_EXPORT constexpr std::uint64_t toUInt64(const char *value)
Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt32(const char *value)
Returns a 32-bit unsigned integer converted from four bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr std::uint16_t toUInt16(const char *value)
Returns a 16-bit unsigned integer converted from two bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT T toInt(const char *value)
Returns the specified (unsigned) integer converted from the specified char array.
CPP_UTILITIES_EXPORT constexpr std::int32_t toInt32(const char *value)
Returns a 32-bit signed integer converted from four bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr std::int64_t toInt64(const char *value)
Returns a 64-bit signed integer converted from eight bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT constexpr std::uint32_t toUInt24(const char *value)
Returns a 32-bit unsigned integer converted from three bytes at a specified position in a char array.
CPP_UTILITIES_EXPORT void getBytes(T value, char *outputbuffer)
Stores the specified (unsigned) integer value in a char array.
CPP_UTILITIES_EXPORT double toFloat64(const char *value)
Returns a 64-bit floating point number converted from eight bytes at a specified position in a char a...
CPP_UTILITIES_EXPORT void getBytes24(std::uint32_t value, char *outputbuffer)
Stores the specified 24-bit unsigned integer value at a specified position in a char array.
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.
STL namespace.
constexpr int i