// Represent // // Douglas Thrift // // $Id$ #include "Hexadecimal.hpp" Hexadecimal::Hexadecimal(const ext::String& string, bool signed_) : Binary(string.GetSize() / 2, 0) { if (string.IsEmpty()) { bytes.InsertLast(0); return; } size_t index(string.GetSize() % 2), offset(index); _rforeach (ext::Vector, byte, bytes) _rfor (byte_t, half, 0, 2) *byte |= hex(string[index++]) << half * 4; if (offset != 0) { bytes.InsertLast(0); bytes.Last() |= hex(string[0]); bytes.Last() |= signed_ && bytes.Last() > 7 ? 0xF0 : 0; } } Hexadecimal::operator ext::String() const { ext::String string; _rforeach (const ext::Vector, byte, bytes) _rfor (byte_t, half, 0, 2) { byte_t hex(0xF & *byte >> half * 4); if (hex < 0xA) hex += '0'; else hex += 'A' - 0xA; string.InsertLast(hex); } return string; } inline byte_t Hexadecimal::hex(const ext::CodePoint& atom) { 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; }