generated from Coding-Cuddles/bootstrap-cpp-kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_converter.h
54 lines (45 loc) · 1.35 KB
/
bit_converter.h
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <cstring>
#include <vector>
#include <cstdint>
#include <vector>
class BitConverter {
public:
static std::vector<unsigned char> int16_to_bytes(int16_t value)
{
return int_to_bytes(value, sizeof(int16_t));
}
static std::vector<unsigned char> uint16_to_bytes(uint16_t value)
{
return int_to_bytes(value, sizeof(uint16_t));
}
static std::vector<unsigned char> int32_to_bytes(int32_t value)
{
return int_to_bytes(value, sizeof(int32_t));
}
static std::vector<unsigned char> uint32_to_bytes(uint32_t value)
{
return int_to_bytes(value, sizeof(uint32_t));
}
static std::vector<unsigned char> int64_to_bytes(int64_t value)
{
return int_to_bytes(value, sizeof(int64_t));
}
static std::vector<unsigned char> uint64_to_bytes(uint64_t value)
{
return int_to_bytes(value, sizeof(uint64_t));
}
static int64_t bytes_to_int64(const std::vector<unsigned char>& byte_list)
{
int64_t value = 0;
std::memcpy(&value, byte_list.data(), sizeof(int64_t));
return value;
}
private:
template <typename T> static std::vector<unsigned char> int_to_bytes(T value, size_t length)
{
std::vector<unsigned char> bytes(length);
std::memcpy(bytes.data(), &value, length);
return bytes;
}
};