summaryrefslogtreecommitdiffhomepage
path: root/byteorder.cpp
blob: 16f7537c5acaae16e6e0a8b3dc12e3377c342871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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())));
}