c++ - Hexadecimal representations of 32-bit numbers -
i have script generates 2 random 32-bit floating point numbers , b, , divides them create output c.
i store 3 of these floating point numbers in hexadecimal format, string contains 8 characters.
i found clever way online here:
http://forums.devshed.com/c-programming-42/printing-a-float-as-a-hex-number-567826.html
and here's implementation of advice in c++
fileout << hex << *(int*)&a[i] << endl; fileout << hex << *(int*)&b[i] << endl; fileout << hex << *(int*)&c[i] << endl;
this works of cases. however, of cases, strings not 8 characters wide. 1 bit long. here sample of output:
af1fe786 ffbbff0b fffbff0b 7fbcbf00 <-i it, has zeros @ end 77fefe77 7ffcbf00 fdad974d f2fc7fef 4a2fff56 67de7744 fdf7711b a9662905 cd7adf0 <-- problem 5f79ffc0 0 <--- problem 6ebbc784 cffffb83 de3bcacf e7b3de77 ec7f660b 3ab44ae4 aefdef82 fffa9fd6 fd1ff7d2 62f4 <--why not "62f40000" ebbf0fa6 ddd78b8d 4d62ebb3 ff5bbceb 3dfc3f61 ff800000 <- zeros @ end, still 8 bytes? df35b371 e0ff7bf1 3db6115d fbbfbccc ddf69e06 5d470843 a3bdae71 fe3fff66 0 <--problem 979e5ba1 febbe3b9 0 <-problem fdf73a80 efcf77a7 4d9887fd cafdfb07 bf7f3f35 4afebadd bffdee35 efb79f7f fb1028c <--problem
i want 8-character representations. case of zero, want convert "00000000".
but confused ones 4, 5, 6, 7 characters long. why numbers 0 filled @ end , others truncated? if int 32 bits, why 1 bit show up? due infamous "subnormal" numbers?
thanks.
if understand correctly requirement, want add together zeros filling character left of hexadecimal representations if number of digits less 8.
in case, can utilize std::setfill()
std::setw()
stream manipulators:
#include <iomanip> // necessary setw , setfill int n = ...; std::cout << std::hex << std::setw(8) << std::setfill('0') << n;
for n = 1024
, instance, output be:
00000400
c++ c string floating-point
No comments:
Post a Comment