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