#include #include "num2bin.h" using namespace std; namespace ca_uwaterloo_alumni_dwharder { bool check() { float x = 1.0; char *pcx = reinterpret_cast( &x ); return (pcx[0] == '?'); } bool is_big_endian = check(); std::string to_hex( char n ) { switch (n) { case 0: return "0000"; case 1: return "0001"; case 2: return "0010"; case 3: return "0011"; case 4: return "0100"; case 5: return "0101"; case 6: return "0110"; case 7: return "0111"; case 8: return "1000"; case 9: return "1001"; case 10: return "1010"; case 11: return "1011"; case 12: return "1100"; case 13: return "1101"; case 14: return "1110"; case 15: return "1111"; } return ""; } std::string to_hex3( char n ) { switch (n) { case 0: return "000"; case 1: return "001"; case 2: return "010"; case 3: return "011"; case 4: return "100"; case 5: return "101"; case 6: return "110"; case 7: return "111"; } return ""; } template std::string num2bin( T a ) { char *f = reinterpret_cast( &a ); std::string s = ""; if ( is_big_endian ) { for ( unsigned int i = 0; i < sizeof( T ); ++i ) { s += to_hex( (f[i] >> 4) & 15 ); s += to_hex( f[i] & 15 ); } } else { for ( unsigned int i = sizeof( T ) - 1; i != 0; --i ) { s += to_hex( (f[i] >> 4) & 15 ); s += to_hex( f[i] & 15 ); } s += to_hex( (f[0] >> 4) & 15 ); s += to_hex( f[0] & 15 ); } return s; } template <> std::string num2bin( float a ) { char *f = reinterpret_cast( &a ); if ( is_big_endian ) { std::string s = (f[0] & 128) ? "1 " : "0 "; s += to_hex3( (f[0] >> 4) & 15 ); s += to_hex( f[0] & 15 ); s += (f[1] & 128) ? "1" : "0"; s += "-"; s += to_hex3( (f[1] >> 4) & 7 ); s += to_hex( f[1] & 15 ); for ( int i = 2; i < 4; ++i ) { s += to_hex( (f[i] >> 4) & 15 ); s += to_hex( f[i] & 15 ); } return s; } else { std::string s = (f[3] & 128) ? "1 " : "0 "; s += to_hex3( (f[3] >> 4) & 15 ); s += to_hex( f[3] & 15 ); s += (f[2] & 128) ? "1" : "0"; s += "-"; s += to_hex3( (f[2] >> 4) & 7 ); s += to_hex( f[2] & 15 ); for ( int i = 1; i >= 0; --i ) { s += to_hex( (f[i] >> 4) & 15 ); s += to_hex( f[i] & 15 ); } return s; } } template <> std::string num2bin( double a ) { char *f = reinterpret_cast( &a ); if ( is_big_endian ) { std::string s = (f[0] & 128) ? "1 " : "0 "; s += to_hex3( (f[0] >> 4) & 15 ); s += to_hex( f[0] & 15 ); s += to_hex( (f[1] >> 4) & 15 ); s += "-"; s += to_hex( f[1] & 15 ); for ( int i = 2; i < 8; ++i ) { s += to_hex( (f[i] >> 4) & 15 ); s += to_hex( f[i] & 15 ); } return s; } else { std::string s = (f[7] & 128) ? "1 " : "0 "; s += to_hex3( (f[7] >> 4) & 15 ); s += to_hex( f[7] & 15 ); s += to_hex( (f[6] >> 4) & 15 ); s += "-"; s += to_hex( f[6] & 15 ); for ( int i = 5; i >= 0; --i ) { s += to_hex( (f[i] >> 4) & 15 ); s += to_hex( f[i] & 15 ); } return s; } } template std::string num2bin( int a ); template std::string num2bin( unsigned int a ); template std::string num2bin( short a ); template std::string num2bin( unsigned short a ); template std::string num2bin( long a ); template std::string num2bin( unsigned long a ); template std::string num2bin( char a ); template std::string num2bin( signed char a ); }