David said:
I have following code.
float b;
b= 1.234;
cout<<hex<<b<<endl;
How come it cannot ouput the hex representation of b?
The hex format is for integer values. A "hex
real number" doesn't mean anything. If you want
to look at the bit pattern of a type 'float' object,
expressed in hex:
#include <algorithm>
#include <ios>
#include <iostream>
#include <iterator>
int main()
{
float f(1.234f);
unsigned char raw[sizeof f];
*(float*)raw = f;
std::cout << std::hex;
std::copy(raw, raw + sizeof raw,
std:
stream_iterator<unsigned int>(std::cout, ""));
std::cout.put('\n');
return 0;
}
But note that the output will vary among platforms,
as not all use the same representation for floating
point values.
-Mike