Newer
Older
/**
* This implementation is inspired by BitConverter from .NET
* https://referencesource.microsoft.com/#mscorlib/system/bitconverter.cs
*/
#include <assert.h>
#include <vector>
#include <endian.h>
#include <string>
#include "custom_types.h"
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
short bytes_to_short(const std::vector<byte> &bytes, const uint fromIndex = 0)
{
assert(bytes.size() >= 2);
assert(fromIndex <= bytes.size() - 2);
// Little endian
auto bit = &bytes[fromIndex];
if (fromIndex % 2 == 0)
{
return (*((short *)bit));
}
else
{
return (short)((*bit) | (*(bit + 1) << 8));
}
}
int bytes_to_int(const std::vector<byte> &bytes, const uint fromIndex = 0)
{
assert(bytes.size() >= 4);
assert(fromIndex <= bytes.size() - 4);
// Little endian
auto bit = &bytes[fromIndex];
if (fromIndex % 4 == 0)
{
return (*((int *)bit));
}
else
{
return (*bit | (*(bit + 1) << 8) | (*(bit + 2) << 16) | (*(bit + 3) << 24));
}
}
int bytes_to_long(const std::vector<byte> &bytes, const uint fromIndex = 0)
{
assert(bytes.size() >= 8);
assert(fromIndex <= bytes.size() - 8);
// Little endian
auto bit = &bytes[fromIndex];
if (fromIndex % 8 == 0)
{
return (*((long *)bit));
}
else
{
int i1 = (*bit) | (*(bit + 1) << 8) | (*(bit + 2) << 16) | (*(bit + 3) << 24);
int i2 = (*(bit + 4)) | (*(bit + 5) << 8) | (*(bit + 6) << 16) | (*(bit + 7) << 24);
return ((uint)i1 | ((long)i2 << 32));
}
}
ushort bytes_to_ushort(const std::vector<byte> &bytes, const uint fromIndex)
{
return ((ushort)bytes_to_short(bytes, fromIndex));
}
uint bytes_to_uint(const std::vector<byte> &bytes, const uint fromIndex)
{
return ((uint)bytes_to_int(bytes, fromIndex));
}
ulong bytes_to_ulong(const std::vector<byte> &bytes, const uint fromIndex)
{
return ((ulong)bytes_to_long(bytes, fromIndex));
}
char get_hex_value(int x)
{
assert(x >= 0 && x < 16);
return (x < 10) ? ((char)(x + '0')) : ((char)((x - 10) + 'A'));
}
std::string bytes_to_string(const std::vector<byte> &bytes, const uint fromIndex, const uint byteCount)
{
assert((fromIndex >= 0) && (fromIndex <= bytes.size() - byteCount));
if (bytes.size() == 0)
return std::string();
int stringLen = byteCount * 3;
std::vector<char> characters;
characters.resize(stringLen);
int index = fromIndex;
for (int i = 0; i < stringLen; i++)
{
byte b = bytes[index++];
characters[i] = get_hex_value(b / 16);
characters[i + 1] = get_hex_value(b % 16);
characters[i + 2] = '-';
}
return std::string(characters.begin(), characters.end() - 1);
}
std::string bytes_to_raw_string(const std::vector<byte> &bytes, const uint fromIndex, const uint byteCount)
{
assert((fromIndex >= 0) && (fromIndex <= bytes.size() - byteCount));
if (bytes.size() == 0)
return std::string();
std::vector<byte> stringBytes(bytes.begin() + fromIndex, bytes.begin() + fromIndex + byteCount);
std::string result(reinterpret_cast<const char *>(stringBytes.data()));
return result;
}
std::string utf8bytes_to_string(const std::vector<byte> &bytes, const uint fromIndex, const uint byteCount)
{
auto fromIt = bytes.begin() + fromIndex;
std::vector<byte> stringBytes(fromIt, fromIt + byteCount);
std::string result = boost::locale::conv::from_utf<char>((char *)stringBytes.data(), "UTF-8");
return result;