diff options
author | Erik Schnetter <schnetter@gmail.com> | 2013-06-02 21:37:19 -0400 |
---|---|---|
committer | Erik Schnetter <schnetter@gmail.com> | 2013-06-02 21:37:19 -0400 |
commit | 6e4088985ea6d9b4ac40a4ff51282ddc364101d9 (patch) | |
tree | 12bec9aefe9846df665c965e7ff1038c43492eea | |
parent | 5ae1d5a0b4c8bc98c388e2b4018eb7ab74553cfe (diff) | |
download | vecmathlib-6e4088985ea6d9b4ac40a4ff51282ddc364101d9.zip vecmathlib-6e4088985ea6d9b4ac40a4ff51282ddc364101d9.tar.gz |
Take endian-ness into account when producing hexadecimal output for floating point numbers
-rw-r--r-- | test.cc | 15 |
1 files changed, 13 insertions, 2 deletions
@@ -73,15 +73,26 @@ struct vecmathlib_test { + static bool is_big_endian() + { + const int i = 1; + unsigned char cs[sizeof i]; + memcpy(cs, &i, sizeof i); + return cs[0]==0; + } + template<typename T> - static string hex(T x) + static string hex(const T x) { unsigned char cs[sizeof x]; memcpy(cs, &x, sizeof x); ostringstream buf; buf << "0x"; const char *const hexdigits = "0123456789abcdef"; - for (size_t n=0; n<sizeof x; ++n) { + const int n0 = is_big_endian() ? 0 : sizeof x - 1; + const int dn = is_big_endian() ? +1 : -1; + const int n1 = n0 + sizeof x * dn; + for (int n=n0; n!=n1; n+=dn) { buf << hexdigits[cs[n]>>4] << hexdigits[cs[n]&15]; } return buf.str(); |