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