// Represent // // Douglas Thrift // // $Id$ #include "Binary.hpp" #ifdef _WIN32 #pragma warning(disable:4267) #endif Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0) { if (string.IsEmpty()) { bytes.InsertLast(0); return; } size_t index(string.GetSize() % 8), offset(index); _rforeach (ext::Vector, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit; if (offset != 0) { bytes.InsertLast(0); _rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit; index = 0; _rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit; } } Binary::operator ext::String() const { ext::String string; _rforeach (const ext::Vector, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0'); return string; }