Repository navigation
Expand file tree
/
Copy pathDynamicInt8.cpp
More file actions
75 lines (57 loc) · 1.49 KB
/
Copy pathDynamicInt8.cpp
File metadata and controls
75 lines (57 loc) · 1.49 KB
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
#include "DynamicInt8.h"
#include <QtMath>
const quint8 DynamicInt8::False;
DynamicInt8::DynamicInt8()
{
write(0);
}
DynamicInt8::DynamicInt8(const uint &number)
{
write(number); // todo (do we apply negative numbers?)
}
bool DynamicInt8::hasNextByte(const uint &index) const
{
if(index >= mBytes.size())
return false;
return mBytes.at(index) & 0x80; // read 1-st bit (MSB)
}
void DynamicInt8::write(const int &number)
{
mBytes.clear();
quint8 byteCount = 1;
if(number != 0)
byteCount = static_cast<quint8>(+1 + log(number)/ log(128));
mBytes.resize(byteCount);
for(quint8 i=0; i < byteCount; i++) {
mBytes[i] = (number >> 7*(byteCount -i -1)) & 0xFF;
mBytes[i] |= 0x80; // set 1-st bit, so we know it has next byte
}
mBytes.back() &= 0x7F; // unset 1-st bit to mark byte as last
}
void DynamicInt8::clear()
{
mBytes.clear();
}
void DynamicInt8::pushBack(const quint8 &value)
{
mBytes.push_back(value);
}
uint DynamicInt8::asUint() const
{
uint result = 0;
for(uint i=0; i < mBytes.size(); i++) {
result += (mBytes.at(i) & 0x7Fu) // unset 1st bits lShift by 7
<< 7*(mBytes.size() -i -1); // leftShift by 7* 0,1,2,3,4 etc.
}
return result;
}
quint8 DynamicInt8::at(const uchar &byteIndex) const
{
if(byteIndex >= uchar(mBytes.size()))
return 0;
return mBytes.at(uint(byteIndex));
}
uchar DynamicInt8::byteCount() const
{
return uchar(mBytes.size());
}