31 #ifndef HELPER_BYTES_HPP_ 32 #define HELPER_BYTES_HPP_ 47 using Bytes = std::vector<std::uint8_t>;
129 std::int64_t
bytesToInt64(
const Bytes& bytes, std::size_t& pos, std::size_t len);
140 std::array<std::uint8_t, sizeEight>
uInt64ToBytes(std::uint64_t number);
141 std::array<std::uint8_t, sizeEight>
int64ToBytes(std::int64_t number);
142 std::array<std::uint8_t, sizeFour>
uInt32ToBytes(std::uint32_t number);
143 std::array<std::uint8_t, sizeFour>
int32ToBytes(std::int32_t number);
144 std::array<std::uint8_t, sizeTwo>
uInt16ToBytes(std::uint16_t number);
145 std::array<std::uint8_t, sizeTwo>
int16ToBytes(std::int16_t number);
146 std::array<std::uint8_t, sizeEight>
doubleToBytes(
double number);
171 const std::uint8_t one{1};
173 return *(
reinterpret_cast<const char *
>(&one)) != 1;
183 const auto minusOne{-1.F};
185 return *(
reinterpret_cast<const std::uint8_t *
>(&minusOne)) > 0;
204 std::array<std::uint8_t, sizeEight> numberBytes{
222 | static_cast<std::uint64_t>(numberBytes[
first]);
240 throw std::invalid_argument(
241 "Bytes::bytesToUInt64():" 242 " Only numbers up to a length of eight bytes are supported (len=" 243 + std::to_string(len)
248 std::array<std::uint8_t, sizeEight> numberBytes{};
250 for(std::size_t n{}; n < len; ++n) {
251 numberBytes.at(n) = bytes[pos + n];
267 | static_cast<std::uint64_t>(numberBytes[
first]);
284 const std::uint64_t withoutSign{
bytesToUInt64(bytes, pos, len)};
286 return *
reinterpret_cast<const std::int64_t *
>(&withoutSign);
303 return *
reinterpret_cast<const std::int64_t *
>(&withoutSign);
318 std::array<std::uint8_t, sizeFour> numberBytes{
331 | static_cast<std::uint32_t>(numberBytes[
first]);
348 return *
reinterpret_cast<const std::int32_t *
>(&withoutSign);
363 std::array<std::uint8_t, sizeTwo> numberBytes{
374 | static_cast<std::uint16_t>(numberBytes[
first]);
391 return *
reinterpret_cast<const std::int16_t *
>(&withoutSign);
407 std::array<std::uint8_t, sizeEight> numberBytes{
418 return reinterpret_cast<const double&
>(*(numberBytes.data()));
432 inline std::array<std::uint8_t, sizeEight>
uInt64ToBytes(std::uint64_t number) {
433 std::array<std::uint8_t, sizeEight> result{};
435 reinterpret_cast<std::uint64_t&
>(*result.data()) = number;
451 inline std::array<std::uint8_t, sizeEight>
int64ToBytes(std::int64_t number) {
452 std::array<std::uint8_t, sizeEight> result{};
454 reinterpret_cast<std::int64_t&
>(*result.data()) = number;
470 inline std::array<std::uint8_t, sizeFour>
uInt32ToBytes(std::uint32_t number) {
471 std::array<std::uint8_t, sizeFour> result{};
473 reinterpret_cast<std::uint32_t&
>(*result.data()) = number;
489 inline std::array<std::uint8_t, sizeFour>
int32ToBytes(std::int32_t number) {
490 std::array<std::uint8_t, sizeFour> result{};
492 reinterpret_cast<std::int32_t&
>(*result.data()) = number;
508 inline std::array<std::uint8_t, sizeTwo>
uInt16ToBytes(std::uint16_t number) {
509 std::array<std::uint8_t, sizeTwo> result{};
511 reinterpret_cast<std::uint16_t&
>(*result.data()) = number;
527 inline std::array<std::uint8_t, sizeTwo>
int16ToBytes(std::int16_t number) {
528 std::array<std::uint8_t, sizeTwo> result{};
530 reinterpret_cast<std::int16_t&
>(*result.data()) = number;
548 std::array<std::uint8_t, sizeEight> result{};
550 reinterpret_cast<double&
>(*result.data()) = number;
573 constexpr std::array hexChars{
574 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' 577 constexpr
auto resultSize{4};
578 constexpr
auto hex240{0xF0};
579 constexpr
auto hex15{0xF};
580 constexpr
auto hexShift{4};
582 std::string result{
"0x"};
584 result.reserve(resultSize);
586 result.push_back(hexChars.at(((byte & hex240) >> hexShift)));
587 result.push_back(hexChars.at(byte & hex15));
633 if(std::isprint(c) != 0) {
634 return std::string(c, 1);
std::array< std::uint8_t, sizeFour > uInt32ToBytes(std::uint32_t number)
Converts an unsigned 32-bit number to an array of four bytes.
Definition: Bytes.hpp:470
constexpr auto sizeTwo
Size of two bytes.
Definition: Bytes.hpp:108
constexpr auto fifth
Index of the fifth byte.
Definition: Bytes.hpp:69
constexpr auto threeBytesBits
Three bytes in bits.
Definition: Bytes.hpp:87
constexpr auto fourBytesBits
Four bytes in bits.
Definition: Bytes.hpp:90
constexpr auto third
Index of the third byte.
Definition: Bytes.hpp:63
std::int32_t bytesToInt32(const Bytes &bytes, std::size_t &pos)
Retrieve a signed 32-bit number from a vector of bytes.
Definition: Bytes.hpp:345
Namespace for global byte operation helper functions.
Definition: Bytes.hpp:44
bool isBigEndian() noexcept
Returns whether the machine running this code uses big endianness.
Definition: Bytes.hpp:170
std::array< std::uint8_t, sizeFour > int32ToBytes(std::int32_t number)
Converts an signed 32-bit number to an array of four bytes.
Definition: Bytes.hpp:489
constexpr auto fourth
Index of the fourth byte.
Definition: Bytes.hpp:66
double bytesToDouble(const Bytes &bytes, std::size_t &pos)
Retrieves a IEEE 754 double-precision binary floating-point number from a vector of bytes...
Definition: Bytes.hpp:406
std::vector< std::uint8_t > Bytes
Definition: Bytes.hpp:47
constexpr auto seventh
Index of the seventh byte.
Definition: Bytes.hpp:75
std::uint16_t bytesToUInt16(const Bytes &bytes, std::size_t &pos)
Retrieve an unsigned 16-bit number from a vector of bytes.
Definition: Bytes.hpp:362
constexpr auto fiveBytesBits
Five bytes in bits.
Definition: Bytes.hpp:93
constexpr auto sixBytesBits
Six bytes in bits.
Definition: Bytes.hpp:96
bool isFloatBigEndian() noexcept
Returns whether the machine running this code uses big endianness for floating-point numbers...
Definition: Bytes.hpp:182
constexpr auto first
Index of the first byte.
Definition: Bytes.hpp:57
std::string byteToHexString(std::uint8_t byte)
Converts a byte to a string containing the byte in hexadecimal format.
Definition: Bytes.hpp:572
std::array< std::uint8_t, sizeEight > int64ToBytes(std::int64_t number)
Converts a signed 64-bit number to an array of eight bytes.
Definition: Bytes.hpp:451
static T::size_type bytes(const T &container)
Returns the number of bytes in an iterable container.
Definition: Container.hpp:144
std::string charToString(char c)
Converts a character to a string.
Definition: Bytes.hpp:609
constexpr auto second
Index of the second byte.
Definition: Bytes.hpp:60
std::int64_t bytesToInt64(const Bytes &bytes, std::size_t &pos)
Retrieve a signed 64-bit number from a vector of bytes.
Definition: Bytes.hpp:300
std::uint64_t bytesToUInt64(const Bytes &bytes, std::size_t &pos)
Retrieve an unsigned 64-bit number from a vector of bytes.
Definition: Bytes.hpp:203
std::array< std::uint8_t, sizeTwo > uInt16ToBytes(std::uint16_t number)
Converts an unsigned 16-bit number to an array of two bytes.
Definition: Bytes.hpp:508
constexpr auto sevenBytesBits
Seven bytes in bits.
Definition: Bytes.hpp:99
std::array< std::uint8_t, sizeEight > doubleToBytes(double number)
Converts a floating-point number with double precision to an array of four bytes. ...
Definition: Bytes.hpp:547
constexpr auto sizeEight
Size of eight bytes.
Definition: Bytes.hpp:102
std::int16_t bytesToInt16(const Bytes &bytes, std::size_t &pos)
Retrieve a signed 16-bit number from a vector of bytes.
Definition: Bytes.hpp:388
constexpr auto eighth
Index of the eighth byte.
Definition: Bytes.hpp:78
constexpr auto sixth
Index of the sixth byte.
Definition: Bytes.hpp:72
void reverse(std::queue< T > &queue)
Reverses the given queue.
Definition: Queue.hpp:53
std::array< std::uint8_t, sizeEight > uInt64ToBytes(std::uint64_t number)
Converts an unsigned 64-bit number to an array of eight bytes.
Definition: Bytes.hpp:432
std::array< std::uint8_t, sizeTwo > int16ToBytes(std::int16_t number)
Converts an signed 16-bit number to an array of two bytes.
Definition: Bytes.hpp:527
constexpr auto twoBytesBits
Two bytes in bits.
Definition: Bytes.hpp:84
std::uint32_t bytesToUInt32(const Bytes &bytes, std::size_t &pos)
Retrieve an unsigned 32-bit number from a vector of bytes.
Definition: Bytes.hpp:317
constexpr auto sizeFour
Size of four bytes.
Definition: Bytes.hpp:105
constexpr auto oneByteBits
One byte in bits.
Definition: Bytes.hpp:81