diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-11-22 13:00:06 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-11-22 13:00:06 +0100 |
commit | 1fae63de23320a1663b7c591e247ad81852ab6dc (patch) | |
tree | bfe539b7eeb5a4d7b68aa1bf853898d60e34f784 /byteorder.cpp | |
parent | 739297d8895b08a9ecd8e81b01b7ba8e8dc4a8ae (diff) |
Support 16-bit short
Diffstat (limited to 'byteorder.cpp')
-rw-r--r-- | byteorder.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/byteorder.cpp b/byteorder.cpp new file mode 100644 index 0000000..16f7537 --- /dev/null +++ b/byteorder.cpp @@ -0,0 +1,40 @@ +#include "byteorder.h" + +#include <boost/endian/conversion.hpp> + +std::vector<uint8_t> endian::to_little(uint16_t value) +{ + std::vector<uint8_t> result(sizeof(uint16_t)); + *(reinterpret_cast<uint16_t*>(result.data())) = boost::endian::native_to_little(value); + return result; +} + +uint16_t endian::from_little16(const std::vector<uint8_t>& value) +{ + return boost::endian::little_to_native(*(reinterpret_cast<const uint16_t*>(value.data()))); +} + +std::vector<uint8_t> endian::to_little(uint32_t value) +{ + std::vector<uint8_t> result(sizeof(uint32_t)); + *(reinterpret_cast<uint32_t*>(result.data())) = boost::endian::native_to_little(value); + return result; +} + +uint32_t endian::from_little32(const std::vector<uint8_t>& value) +{ + return boost::endian::little_to_native(*(reinterpret_cast<const uint32_t*>(value.data()))); +} + +std::vector<uint8_t> endian::to_little(uint64_t value) +{ + std::vector<uint8_t> result(sizeof(uint64_t)); + *(reinterpret_cast<uint16_t*>(result.data())) = boost::endian::native_to_little(value); + return result; +} + +uint64_t endian::from_little64(const std::vector<uint8_t>& value) +{ + return boost::endian::little_to_native(*(reinterpret_cast<const uint64_t*>(value.data()))); +} + |