// Represent // // Douglas Thrift // // $Id$ #include #include #include #include class Binary { private: std::vector bytes; public: Binary(const std::string& string); template Binary(const Type& type); operator std::string() const; }; Binary::Binary(const std::string& string) { throw; } template Binary::Binary(const Type& type) : bytes(sizeof (type)) { char* type_(reinterpret_cast(const_cast(&type))); _mforeach (std::vector, byte, bytes) *byte = *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'; } } return string; } std::ostream& operator<<(std::ostream& out, const Binary& binary) { return out << std::string(binary); } int main(int argc, char* argv[]) { std::string hello("Hello, World!"); _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, 4294967286, 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 (double index(-1.0); index < 1.0; index += 0.1) { std::cout << Binary(index) << " = " << index << std::endl; } return 0; }