4#ifndef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL
5#error "Do not include binaryconversionprivate.h directly."
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
16#pragma GCC diagnostic push
17#pragma GCC diagnostic ignored "-Wsign-conversion"
25#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
26 return static_cast<std::int16_t
>((
static_cast<std::int16_t
>(value[0]) << 8 & 0xFF00) | (
static_cast<std::int16_t
>(value[1]) & 0x00FF));
28 return static_cast<std::int16_t
>((
static_cast<std::int16_t
>(value[1]) << 8 & 0xFF00) | (
static_cast<std::int16_t
>(value[0]) & 0x00FF));
37#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
38 return static_cast<std::uint16_t
>((
static_cast<std::uint16_t
>(value[0]) << 8 & 0xFF00) | (
static_cast<std::uint16_t
>(value[1]) & 0x00FF));
40 return static_cast<std::uint16_t
>((
static_cast<std::uint16_t
>(value[1]) << 8 & 0xFF00) | (
static_cast<std::uint16_t
>(value[0]) & 0x00FF));
49#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
50 return static_cast<std::int32_t
>((
static_cast<std::int32_t
>(value[0]) << 24 & 0xFF000000)
51 | (
static_cast<std::int32_t
>(value[1]) << 16 & 0x00FF0000) | (
static_cast<std::int32_t
>(value[2]) << 8 & 0x0000FF00)
52 | (
static_cast<std::int32_t
>(value[3]) & 0x000000FF));
54 return static_cast<std::int32_t
>((
static_cast<std::int32_t
>(value[3]) << 24 & 0xFF000000)
55 | (
static_cast<std::int32_t
>(value[2]) << 16 & 0x00FF0000) | (
static_cast<std::int32_t
>(value[1]) << 8 & 0x0000FF00)
56 | (
static_cast<std::int32_t
>(value[0]) & 0x000000FF));
65#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
66 return (
static_cast<std::uint32_t
>(value[0]) << 16 & 0x00FF0000) | (
static_cast<std::uint32_t
>(value[1]) << 8 & 0x0000FF00)
67 | (
static_cast<std::uint32_t
>(value[2]) & 0x000000FF);
69 return (
static_cast<std::uint32_t
>(value[2]) << 16 & 0x00FF0000) | (
static_cast<std::uint32_t
>(value[1]) << 8 & 0x0000FF00)
70 | (
static_cast<std::uint32_t
>(value[0]) & 0x000000FF);
79#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
80 return (
static_cast<std::uint32_t
>(value[0]) << 24 & 0xFF000000) | (
static_cast<std::uint32_t
>(value[1]) << 16 & 0x00FF0000)
81 | (
static_cast<std::uint32_t
>(value[2]) << 8 & 0x0000FF00) | (
static_cast<std::uint32_t
>(value[3]) & 0x000000FF);
83 return (
static_cast<std::uint32_t
>(value[3]) << 24 & 0xFF000000) | (
static_cast<std::uint32_t
>(value[2]) << 16 & 0x00FF0000)
84 | (
static_cast<std::uint32_t
>(value[1]) << 8 & 0x0000FF00) | (
static_cast<std::uint32_t
>(value[0]) & 0x000000FF);
93#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
94 return (
static_cast<std::int64_t
>(value[0]) << 56 & 0xFF00000000000000) | (
static_cast<std::int64_t
>(value[1]) << 48 & 0x00FF000000000000)
95 | (
static_cast<std::int64_t
>(value[2]) << 40 & 0x0000FF0000000000) | (
static_cast<std::int64_t
>(value[3]) << 32 & 0x000000FF00000000)
96 | (
static_cast<std::int64_t
>(value[4]) << 24 & 0x00000000FF000000) | (
static_cast<std::int64_t
>(value[5]) << 16 & 0x0000000000FF0000)
97 | (
static_cast<std::int64_t
>(value[6]) << 8 & 0x000000000000FF00) | (
static_cast<std::int64_t
>(value[7]) & 0x00000000000000FF);
99 return (
static_cast<std::int64_t
>(value[7]) << 56 & 0xFF00000000000000) | (
static_cast<std::int64_t
>(value[6]) << 48 & 0x00FF000000000000)
100 | (
static_cast<std::int64_t
>(value[5]) << 40 & 0x0000FF0000000000) | (
static_cast<std::int64_t
>(value[4]) << 32 & 0x000000FF00000000)
101 | (
static_cast<std::int64_t
>(value[3]) << 24 & 0x00000000FF000000) | (
static_cast<std::int64_t
>(value[2]) << 16 & 0x0000000000FF0000)
102 | (
static_cast<std::int64_t
>(value[1]) << 8 & 0x000000000000FF00) | (
static_cast<std::int64_t
>(value[0]) & 0x00000000000000FF);
111#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
112 return (
static_cast<std::uint64_t
>(value[0]) << 56 & 0xFF00000000000000) | (
static_cast<std::uint64_t
>(value[1]) << 48 & 0x00FF000000000000)
113 | (
static_cast<std::uint64_t
>(value[2]) << 40 & 0x0000FF0000000000) | (
static_cast<std::uint64_t
>(value[3]) << 32 & 0x000000FF00000000)
114 | (
static_cast<std::uint64_t
>(value[4]) << 24 & 0x00000000FF000000) | (
static_cast<std::uint64_t
>(value[5]) << 16 & 0x0000000000FF0000)
115 | (
static_cast<std::uint64_t
>(value[6]) << 8 & 0x000000000000FF00) | (
static_cast<std::uint64_t
>(value[7]) & 0x00000000000000FF);
117 return (
static_cast<std::uint64_t
>(value[7]) << 56 & 0xFF00000000000000) | (
static_cast<std::uint64_t
>(value[6]) << 48 & 0x00FF000000000000)
118 | (
static_cast<std::uint64_t
>(value[5]) << 40 & 0x0000FF0000000000) | (
static_cast<std::uint64_t
>(value[4]) << 32 & 0x000000FF00000000)
119 | (
static_cast<std::uint64_t
>(value[3]) << 24 & 0x00000000FF000000) | (
static_cast<std::uint64_t
>(value[2]) << 16 & 0x0000000000FF0000)
120 | (
static_cast<std::uint64_t
>(value[1]) << 8 & 0x000000000000FF00) | (
static_cast<std::uint64_t
>(value[0]) & 0x00000000000000FF);
129 const auto val =
toInt32(value);
130 const auto *
const c =
reinterpret_cast<const char *
>(&val);
131 return *
reinterpret_cast<const float *
>(c);
139 const auto val =
toInt64(value);
140 const auto *
const c =
reinterpret_cast<const char *
>(&val);
141 return *
reinterpret_cast<const double *
>(c);
152template <
class T, Traits::EnableIf<std::is_
integral<T>> * =
nullptr>
CPP_UTILITIES_EXPORT inline T
toInt(
const char *value)
155 std::memcpy(&dst, value,
sizeof(T));
156#ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
168#if CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL == 0
169 outputbuffer[0] =
static_cast<char>((value >> 16) & 0xFF);
170 outputbuffer[1] =
static_cast<char>((value >> 8) & 0xFF);
171 outputbuffer[2] =
static_cast<char>((value) & 0xFF);
173 outputbuffer[2] =
static_cast<char>((value >> 16) & 0xFF);
174 outputbuffer[1] =
static_cast<char>((value >> 8) & 0xFF);
175 outputbuffer[0] =
static_cast<char>((value) & 0xFF);
186template <
class T, Traits::EnableIf<std::is_
integral<T>> * =
nullptr>
CPP_UTILITIES_EXPORT inline void getBytes(T value,
char *outputbuffer)
188#ifdef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
191 std::memcpy(outputbuffer, &value,
sizeof(T));
199 auto *c =
reinterpret_cast<char *
>(&value);
200 auto i = *
reinterpret_cast<std::int32_t *
>(c);
209 auto *c =
reinterpret_cast<char *
>(&value);
210 auto i = *
reinterpret_cast<std::int64_t *
>(c);
215#pragma GCC diagnostic pop
218#undef CONVERSION_UTILITIES_BINARY_CONVERSION_INTERNAL_NEEDS_SWAP
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
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 constexpr std::int16_t toInt16(const char *value)
Returns a 16-bit signed integer converted from two bytes at a specified position 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.