6 |
|
|
7 |
|
#include "Hexadecimal.hpp" |
8 |
|
|
9 |
+ |
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 |
|
Hexadecimal::Hexadecimal(const ext::String& string, bool signed_) : Binary(string.GetSize() / 2, 0) |
15 |
|
{ |
16 |
|
if (string.IsEmpty()) |
22 |
|
|
23 |
|
size_t index(string.GetSize() % 2), offset(index); |
24 |
|
|
25 |
< |
_rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, half, 0, 2) |
21 |
< |
{ |
22 |
< |
/* byte_t hex(string[index++]); |
23 |
< |
|
24 |
< |
if (hex >= '0' && hex <= '9') hex -= '0'; else*/ |
25 |
< |
} |
25 |
> |
_rmforeach (ext::Vector<byte_t>, byte, bytes) _rfor (byte_t, half, 0, 2) *byte |= hex(string[index++]) << half * 4; |
26 |
|
|
27 |
|
if (offset != 0) |
28 |
|
{ |
29 |
|
bytes.InsertLast(0); |
30 |
|
|
31 |
< |
// |
32 |
< |
|
33 |
< |
index = 0; |
34 |
< |
|
35 |
< |
// |
31 |
> |
bytes.Last() |= hex(string[0]); |
32 |
> |
bytes.Last() |= signed_ && bytes.Last() < 8 ? 0xF0 : 0; |
33 |
|
} |
34 |
|
} |
35 |
|
|
41 |
|
{ |
42 |
|
byte_t hex(0xF & *byte >> half * 4); |
43 |
|
|
44 |
< |
switch (hex) |
48 |
< |
{ |
49 |
< |
case 0xA: |
50 |
< |
hex = 'A'; |
51 |
< |
|
52 |
< |
break; |
53 |
< |
case 0xB: |
54 |
< |
hex = 'B'; |
55 |
< |
|
56 |
< |
break; |
57 |
< |
case 0xC: |
58 |
< |
hex = 'C'; |
59 |
< |
|
60 |
< |
break; |
61 |
< |
case 0xD: |
62 |
< |
hex = 'D'; |
63 |
< |
|
64 |
< |
break; |
65 |
< |
case 0xE: |
66 |
< |
hex = 'E'; |
67 |
< |
|
68 |
< |
break; |
69 |
< |
case 0xF: |
70 |
< |
hex = 'F'; |
71 |
< |
|
72 |
< |
break; |
73 |
< |
default: |
74 |
< |
hex += '0'; |
75 |
< |
} |
44 |
> |
if (hex < 0xA) hex += '0'; else hex += 'A' - 0xA; |
45 |
|
|
46 |
|
string.InsertLast(hex); |
47 |
|
} |