-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarymanipulator.cpp
174 lines (161 loc) · 4.63 KB
/
binarymanipulator.cpp
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "binarymanipulator.h"
#include <iterator>
#include <string>
#include <stdexcept>
#include <vector>
namespace BinaryManipulator
{
std::string HexToBinaryString(int hex)
{
//Make sure the byte passed in is valid
if (hex > 0xFF)
throw "Value cannot be greater than 0xFF!";
else if (hex < 0x00)
throw "Value cannot be less than 0x00!";
std::string binary = "";
for (int i = 128; i >= 1; i /= 2)
{
hex -= i;
if (hex >= 0)
{
binary += "1";
}
else
{
hex += i;
binary += "0";
}
}
return binary;
}
std::vector<bool> HexToBitVector(int hex)
{
std::vector<bool> bits;
for (int i = 0; i < 8; i++)
{
bits.push_back(false);
}
//Make sure the byte passed in is valid
if (hex < 0x0 || hex > 0xFF)
{
throw "A value between 0x0 and 0xF is expected";
}
for (int i = 128, j = 0; i >= 1; i /= 2, ++j)
{
hex -= i;
if (hex >= 0)
{
bits.at(j) = true;
}
else
{
hex += i;
bits.at(j) = false;
}
}
return bits;
}
std::vector<bool> HexDigitToBitVector(int hexDigit)
{
std::vector<bool> bits;
for (int i = 0; i < 4; i++)
{
bits.push_back(false);
}
//Make sure the byte passed in is valid
if (hexDigit < 0x0 || hexDigit > 0xF)
{
throw "A value between 0x0 and 0xF is expected";
}
for (int i = 8, j = 0; i >= 1; i /= 2, ++j)
{
hexDigit -= i;
if (hexDigit >= 0)
{
bits.at(j) = true;
}
else
{
hexDigit += i;
bits.at(j) = false;
}
}
return bits;
}
int BinaryStringToHex(const std::string &binary)
{
int value = 0;
int j = 1;
for (int i = binary.length() - 1; i >= 0; i--)
{
if (binary[i] == '1')
value += j;
j *= 2;
}
return value;
}
int BitVectorToHex(std::vector<bool> bits, int start, int end)
{
if (end >= static_cast<int>(bits.size()))
throw "End cannot be greater than the bit vector size!";
int value = 0;
int j = 1;
for (int i = end; i >= start; --i)
{
if (bits.at(i) == true)
value += j;
j *= 2;
}
return value;
}
int BitVectorToHex(std::vector<bool> bits)
{
return BitVectorToHex(bits, 0, (bits.size() - 1));
}
std::string BitVectorToString(std::vector<bool> &bits)
{
std::string binary = "";
for (std::vector<bool>::iterator itor = bits.begin(); itor != bits.end(); ++itor)
{
if (*itor)
binary += "1";
else
binary += "0";
}
return binary;
}
void WriteHexDigitToBitVector(std::vector<bool> &bits, unsigned int start, int hexDigit)
{
WriteHexDigitToBitVector(bits, start, hexDigit, 0, 3);
}
void WriteHexDigitToBitVector(std::vector<bool> &bits, unsigned int start, int hexDigit, unsigned int hexStart, unsigned int hexEnd)
{
if (start > bits.size())
throw "Start must be a proper index";
else if (hexDigit < 0x0 || hexDigit > 0xF)
throw "A hex digit is expected";
else if (hexStart > 3 || hexEnd > 3)
throw "Start and end must be between 0 and 3";
std::vector<bool> hexDigitBits = HexDigitToBitVector(hexDigit);
if (start + hexDigitBits.size() > bits.size())
throw "Cannot write outside of the bit array";
for (unsigned int i = start, j = hexStart; j <= hexEnd; i++, j++)
{
bits.at(i) = hexDigitBits.at(j);
}
}
void WriteByteToBitVector(std::vector<bool> &bits, unsigned int start, int hex)
{
if (start > bits.size())
throw "Start must be a proper index";
else if (hex < 0x00 || hex > 0xFF)
throw "A hex digit is expected";
std::vector<bool> hexBits = HexToBitVector(hex);
if (start + hexBits.size() > bits.size())
throw "Cannot write outside of the bit array";
for (unsigned int i = start, j = 0; i < start + bits.size(); ++i, ++j)
{
bits.at(i) = hexBits.at(j);
}
}
}