ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Hexadecimal.cpp
Revision: 373
Committed: 2004-12-22T17:27:28-08:00 (20 years, 6 months ago) by douglas
File size: 1092 byte(s)
Log Message:
Even better!

File Contents

# Content
1 // Represent
2 //
3 // Douglas Thrift
4 //
5 // $Id$
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())
17 {
18 bytes.InsertLast(0);
19
20 return;
21 }
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) *byte |= hex(string[index++]) << half * 4;
26
27 if (offset != 0)
28 {
29 bytes.InsertLast(0);
30
31 bytes.Last() |= hex(string[0]);
32 bytes.Last() |= signed_ && bytes.Last() < 8 ? 0xF0 : 0;
33 }
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 if (hex < 0xA) hex += '0'; else hex += 'A' - 0xA;
45
46 string.InsertLast(hex);
47 }
48
49 return string;
50 }

Properties

Name Value
svn:eol-style native
svn:keywords Id