--- Represent/represent.cpp 2004/12/21 09:12:18 362 +++ Represent/Binary.cpp 2005/03/10 03:08:17 422 @@ -4,99 +4,42 @@ // // $Id$ -#include -#include -#include +#include "Binary.hpp" -#include +#ifdef _WIN32 +#pragma warning(disable:4267) +#endif -/*template -std::string binary(const Type& type) +Binary::Binary(const ext::String& string, bool signed_) : bytes(string.GetSize() / 8, 0) { - std::ostringstream string; - - _rforu (index, 0, 8 * sizeof (type)) + if (string.IsEmpty()) { - bool bit(1 & type >> index); + bytes.InsertLast(0); - string << bit; + return; } - return string.str(); -}*/ + size_t index(string.GetSize() % 8), offset(index); -class Binary -{ -private: - std::vector bytes; -public: - Binary(const std::string& string); - template - Binary(const Type& type); - operator std::string() const; -}; + _rforeach (ext::Vector, byte, bytes) _rfor (byte_t, bit, 0, 8) *byte |= (string[index++] == '1') << bit; -Binary::Binary(const std::string& string) -{ - throw; -} + if (offset != 0) + { + bytes.InsertLast(0); -template -Binary::Binary(const Type& type) : bytes(sizeof (type)) -{ - char* type_(reinterpret_cast(const_cast(&type))); + _rfor (byte_t, bit, offset, 8) bytes.Last() |= (signed_ && string[0] == '1') << bit; - _mforeach (std::vector, byte, bytes) *byte = *type_++; -} + index = 0; -Binary::operator std::string() const -{ - std::string string; - - _rforeach (std::vector, byte, bytes) - { - _rfor (char, bit, 0, 8) - { - string += 1 & *byte >> bit ? '1' : '0'; - } + _rfor (byte_t, bit, 0, offset) bytes.Last() |= (string[index++] == '1') << bit; } - - return string; -} - -std::ostream& operator<<(std::ostream& out, const Binary& binary) -{ - return out << std::string(binary); } -int main(int argc, char* argv[]) +Binary::operator ext::String() const { - std::string hello("Hello, World!"); + ext::String string; - _foreach (std::string, atom, hello) - { - std::cout << Binary(*atom) << " = " << *atom << std::endl; - } - - _fori (index, -10, 11) - { - std::cout << Binary(index) << " = " << index << std::endl; - } - - _foru (index, -10, 11) - { - std::cout << Binary(index) << " = " << index << std::endl; - } + _rforeach (const ext::Vector, byte, bytes) _rfor (byte_t, bit, 0, 8) string.InsertLast(1 & *byte >> bit ? '1' : '0'); - for (float index(-1.0); index < 1.0; index += 0.1) - { - std::cout << Binary(index) << " = " << index << std::endl; - } - - for (double index(-1.0); index < 1.0; index += 0.1) - { - std::cout << Binary(index) << " = " << index << std::endl; - } - - return 0; + return string; }