The following C++ program takes values of primitive C++ types (e.g. int, double and so on) and converts them into hex and binary representations. This is a simple and useful tool for examining how actual low-level data is represented by the compiler and CPU.
Once compiled, simply provide as arguments the name of the type following by a list of values.
Example:
$ ./cval unsigned int 1 2 4294967295
On an Intel Core 2 Duo CPU running Ubuntu 9.04 (g++ version 4.3.3) an unsigned int has the following representation:
unsigned int, sizeof 4, non-sign-bits 32, digits10 9, min 0, max 4294967295
1 | 00 00 00 01 | 00000000 00000000 00000000 00000001
2 | 00 00 00 02 | 00000000 00000000 00000000 00000010
4294967295 | FF FF FF FF | 11111111 11111111 11111111 11111111The source code is below. Save as cval.cc and compile with g++ cval.cc -o cval :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | /* Darren Smith, 2009 (www.xxdev.com) */ #include <bitset> #include <cstring> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <sstream> /* List of types extracted from system header file include/c++/limits : bool char double float int long long double long long short signed char unsigned char unsigned int unsigned long unsigned long long unsigned short wchar_t */ /* * Template function for getting the name of a type */ template <typename T> std::string name_of_type(); /* Specialisations */ #define SPECIALSE_name_of_type( X ) \ template <> std::string name_of_type< X >() { return #X; } SPECIALSE_name_of_type( bool ); SPECIALSE_name_of_type( char ); SPECIALSE_name_of_type( double ); SPECIALSE_name_of_type( float ); SPECIALSE_name_of_type( int ); SPECIALSE_name_of_type( long ); SPECIALSE_name_of_type( long double ); SPECIALSE_name_of_type( long long ); SPECIALSE_name_of_type( short ); SPECIALSE_name_of_type( signed char ); SPECIALSE_name_of_type( unsigned char ); SPECIALSE_name_of_type( unsigned int ); SPECIALSE_name_of_type( unsigned long ); SPECIALSE_name_of_type( unsigned long long ); SPECIALSE_name_of_type( unsigned short ); SPECIALSE_name_of_type( wchar_t ); /* * Get hex representation of a value */ template<typename T> void to_hex(T _v , std::ostream& s) { union { T value; unsigned char bytes[sizeof(T)]; }; memset(&bytes, 0, sizeof(T)); value = _v; s << std::setfill('0'); s << std::hex; s << std::uppercase; s << std::right; #if BYTE_ORDER == BIG_ENDIAN size_t i = 0; size_t const e = sizeof(T)-1; #else size_t i = sizeof(T)-1; size_t const e = 0; #endif for (;;) { s << std::setw(2) << (unsigned int) bytes[i]; if (i == e) break; s << " "; #if BYTE_ORDER == BIG_ENDIAN ++i; #else --i; #endif } } /* * Get binary representation of a value. We use std::bitset to generate * the actual binary for each byte. */ template <typename T> void to_bin(T _v, std::ostream& os) { std::bitset< std::numeric_limits<unsigned char>::digits > bs; union { T value; unsigned char bytes[sizeof(T)]; }; memset(&bytes, 0, sizeof(T)); value = _v; #if BYTE_ORDER == BIG_ENDIAN size_t i = 0; size_t const e = sizeof(T)-1; #else size_t i = sizeof(T)-1; size_t const e = 0; #endif for (;;) { bs = bytes[i]; os << bs; if (i == e) break; os << " "; #if BYTE_ORDER == BIG_ENDIAN ++i; #else --i; #endif } } /* * Utility methods for a primitive type. */ template <typename T> class typeutil { public: typedef T io_type; static std::string name() { return ::name_of_type<T>; } static T from_string(const char* s, std::ios_base& (*base)(std::ios_base&)) { T retval; std::istringstream iss(s); if ((iss >> base >> retval).fail()) { std::ostringstream oss; oss << "failed to convert \"" << s << "\" to " << ::name_of_type<T>() << " at " __FILE__ << ":" << __LINE__; throw std::ios_base::failure(oss.str()); } return retval; } }; /* * Generic method to use when the conversion from a string to a type * needs to go via an intermediate ordinal type. */ template <typename S, typename I> S convert_via_int(const char* s, std::ios_base& (*base)(std::ios_base&)) { std::istringstream iss(s); I intermediate; if ((iss >> base >> intermediate).fail()) { std::ostringstream oss; oss << "failed to convert \"" << s << "\" to " << ::name_of_type<S>() << " at " __FILE__ << ":" << __LINE__; throw std::ios_base::failure(oss.str()); } return intermediate; } /* Specialisation for char types */ template <> class typeutil<char> { public: typedef int io_type; static std::string name() { return ::name_of_type<char>(); } static char from_string(const char* s, std::ios_base& (*base)(std::ios_base&)) { return ::convert_via_int<char, io_type>(s, base); } }; template <> class typeutil<unsigned char> { public: typedef unsigned int io_type; static std::string name() { return ::name_of_type<unsigned char>(); } static unsigned char from_string(const char* s, std::ios_base& (*base)(std::ios_base&)) { return ::convert_via_int<unsigned char, io_type>(s, base); } }; template <> class typeutil<signed char> { public: typedef signed int io_type; static std::string name() { return ::name_of_type<signed char>(); } static signed char from_string(const char* s, std::ios_base& (*base)(std::ios_base&)) { return ::convert_via_int<signed char, io_type>(s, base); } }; /* Formatted output of a floating point type value */ template <typename T> void pr_float(T v, std::ostream& os, int p, int e, bool pad) { os << std::dec; if (pad) os << std::setw(2+p+2+e); os << std::right << std::setfill(' ') << std::setprecision(p) << v; } /* Formatted output of a value. This function is specialised (below) * for floating point types. */ template <typename T> void pr_raw(T v, std::ostream& os, bool pad) { typename typeutil<T>::io_type o = v; if (pad) { // add 1 for most-sig. position, and add 1 for minus sign os << std::setw( 1+std::numeric_limits<T>::digits10 + std::numeric_limits<T>::is_signed); } os << std::dec << std::right << std::setfill(' ') << o; } template<> void pr_raw(float v, std::ostream& os, bool pad) { pr_float(v, os, 20, 5, pad); } template<> void pr_raw(double v, std::ostream& os, bool pad){ pr_float(v, os, 20, 5, pad); } template<> void pr_raw(long double v, std::ostream& os, bool pad){ pr_float(v, os, 25, 6, pad); } /* For a type T, and a set of values starting at argv[argoffset], * display a summary of the type and the hex and binary * representations of the set of values. */ template <typename T> void dump(int argc, const char** argv, int argoffset, std::ostream& os) { // summary line os << name_of_type<T>() << ", sizeof " << sizeof(T) << ", non-sign-bits " << std::numeric_limits<T>::digits << ", digits10 " << std::numeric_limits<T>::digits10 << ", min "; pr_raw<T>(std::numeric_limits<T>::min(), os, false); os << ", max "; pr_raw<T>(std::numeric_limits<T>::max(), os, false); os << std::endl; // display the bin & hex representations of the values for (int i = argoffset; i < argc; ++i) { T f = typeutil<T>::from_string(argv[i], std::dec); pr_raw<T>(f, os, true); os << " | "; ::to_hex<T>(f, os); os << " | "; to_bin<T>(f, os); os << std::endl; } } #define LOAD_DUMP_FUNC( X ) dump_funs[ ::name_of_type< X >() ] = dump< X >; int main(int argc, const char** argv) { // Build a map of dump functions, one for each type typedef void (*dump_funptr)(int, const char**, int, std::ostream&); std::map<std::string, dump_funptr> dump_funs; LOAD_DUMP_FUNC( bool ); LOAD_DUMP_FUNC( char ); LOAD_DUMP_FUNC( double ); LOAD_DUMP_FUNC( float ); LOAD_DUMP_FUNC( int ); LOAD_DUMP_FUNC( long ); LOAD_DUMP_FUNC( long double ); LOAD_DUMP_FUNC( long long ); LOAD_DUMP_FUNC( short ); LOAD_DUMP_FUNC( signed char ); LOAD_DUMP_FUNC( unsigned char ); LOAD_DUMP_FUNC( unsigned int ); LOAD_DUMP_FUNC( unsigned long ); LOAD_DUMP_FUNC( unsigned long long ); LOAD_DUMP_FUNC( unsigned short ); // LOAD_DUMP_FUNC( wchar_t ); if (argc > 1) { dump_funptr fptr = NULL; int argoffset; if (not fptr and argc > 3) { // eg "unsigned long long" in separate argv[] cells std::ostringstream concat; concat << argv[1] << " " << argv[2] << " " << argv[3]; fptr = dump_funs[ concat.str() ]; argoffset = 4; } if (not fptr and argc > 2) { // eg "unsigned long" in separate argv[] cells std::ostringstream concat; concat << argv[1] << " " << argv[2]; fptr = dump_funs[ concat.str() ]; argoffset = 3; } if (not fptr and argc > 1) { // eg "long" in separate argv[] cells fptr = dump_funs[ argv[1] ]; argoffset = 2; } if (fptr) { try { fptr(argc, argv, argoffset, std::cout); } catch (std::ios_base::failure& ex) { std::cerr << "caught exception: " << ex.what() << "\n"; return 1; } } else { std::cerr << "not a supported type\n"; return 1; } } else { // if no args, display all supported types for (std::map<std::string, dump_funptr>::iterator i = dump_funs.begin(); i != dump_funs.end(); ++i) { std::cout << i->first << "\n"; } } return 0; } |