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