Floats are not expressed the same as chars. They work differently. So while you might expect it to work with integers, it will give you a messed-up float value.
Also, I messed up the example (don't know what I was thinking), sorry.
Here is a better example:
Code:
#include <iostream>
union something
{
float fv;
unsigned char sv[4];
};
int main()
{
something e;
e.sv[0] = 0xFF;
e.sv[1] = 0x00;
e.sv[2] = 0xFF;
e.sv[3] = 0x00;
std::cout << "Float: " << e.fv << std::endl;
std::cout << "Chars: " << std::hex << (int)e.sv[0] << ", " << (int)e.sv[1] << ", " << (int)e.sv[2] << ", " << (int)e.sv[3] << std::endl;
}
Output:
Float: 2.34184e-038
Chars: ff, 0, ff, 0
Another example:
Code:
#include <iostream>
union something
{
float fv;
unsigned char sv[4];
};
int main()
{
something e;
/*e.sv[0] = 0xFF;
e.sv[1] = 0x00;
e.sv[2] = 0xFF;
e.sv[3] = 0x00;*/
e.fv = 50.0f;
std::cout << "Float: " << e.fv << std::endl;
std::cout << "Chars: " << std::hex << (int)e.sv[0] << ", " << (int)e.sv[1] << ", " << (int)e.sv[2] << ", " << (int)e.sv[3] << std::endl;
}
Output:
Float: 50
Chars: 0, 0, 48, 42