--- Represent/represent.cpp 2004/12/21 09:12:18 362 +++ Represent/represent.cpp 2004/12/21 11:32:17 369 @@ -10,35 +10,34 @@ #include -/*template -std::string binary(const Type& type) -{ - std::ostringstream string; - - _rforu (index, 0, 8 * sizeof (type)) - { - bool bit(1 & type >> index); - - string << bit; - } - - return string.str(); -}*/ - class Binary { private: std::vector bytes; public: - Binary(const std::string& string); + Binary(std::string& string, bool signed_ = false); template Binary(const Type& type); + template + Type convert(bool signed_ = false); operator std::string() const; }; -Binary::Binary(const std::string& string) +Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) { - throw; + if (string.find_first_not_of("01") != std::string::npos) throw; + + std::string::size_type off(string.size() % 8); + + if (off != 0) + { + bytes.push_back(0); + string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); + } + + std::string::size_type index(0); + + _rmforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; } template @@ -49,22 +48,30 @@ Binary::Binary(const Type& type) : bytes _mforeach (std::vector, byte, bytes) *byte = *type_++; } +template +Type Binary::convert(bool signed_) +{ + Type type; + + if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); + + char* type_(reinterpret_cast(&type)); + + _foreach (std::vector, byte, bytes) *type_++ = *byte; + + return type; +} + Binary::operator std::string() const { std::string string; - _rforeach (std::vector, byte, bytes) - { - _rfor (char, bit, 0, 8) - { - string += 1 & *byte >> bit ? '1' : '0'; - } - } + _rforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; return string; } -std::ostream& operator<<(std::ostream& out, const Binary& binary) +inline std::ostream& operator<<(std::ostream& out, const Binary& binary) { return out << std::string(binary); } @@ -73,30 +80,24 @@ int main(int argc, char* argv[]) { std::string hello("Hello, World!"); - _foreach (std::string, atom, hello) - { - std::cout << Binary(*atom) << " = " << *atom << std::endl; - } + _foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl; - _fori (index, -10, 11) - { - std::cout << Binary(index) << " = " << index << std::endl; - } + _fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; - _foru (index, -10, 11) - { - std::cout << Binary(index) << " = " << index << std::endl; - } + _foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; - for (float index(-1.0); index < 1.0; index += 0.1) - { - std::cout << Binary(index) << " = " << index << std::endl; - } + 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; - } + for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; + + std::string string("101001101001"); + Binary binary(string, true); + + std::cout << binary << " = " << string << std::endl; + + float value(binary.convert()); + + std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; return 0; }