1 |
// Represent |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Binary.hpp" |
8 |
|
9 |
Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0) |
10 |
{ |
11 |
if (string.IsEmpty()) |
12 |
{ |
13 |
bytes.InsertLast(0); |
14 |
|
15 |
return; |
16 |
} |
17 |
|
18 |
size_t index(string.GetSize() % 8), offset(index); |
19 |
|
20 |
_rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
21 |
|
22 |
if (offset != 0) |
23 |
{ |
24 |
bytes.InsertLast(0); |
25 |
|
26 |
_rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit; |
27 |
|
28 |
index = 0; |
29 |
|
30 |
_rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit; |
31 |
} |
32 |
} |
33 |
|
34 |
Binary::operator ext::String() const |
35 |
{ |
36 |
ext::String string; |
37 |
|
38 |
_rforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0'); |
39 |
|
40 |
return string; |
41 |
} |