Aug 12, 2013 at 2:33am UTC
Hello, I'd like to know how I could break a float into individual bytes for binary use.
Sounds simple, but it seems as though the compiler doesn't realize everything is just a series of bytes.
Thanks in advance.
Aug 12, 2013 at 2:40am UTC
You don't need individual bytes. Here is an example with saving to a file:
1 2 3 4 5 6
{
std::ofstream out ("out.dat" );
float a = 3.14;
double b = 3.1415926;
out.write((char const *)&a, sizeof (a)).write((char const *)&b, sizeof (b));
}
Last edited on Aug 12, 2013 at 2:42am UTC
Aug 12, 2013 at 2:42am UTC
But is that cross platform?
Aug 12, 2013 at 2:43am UTC
Using floats and doubles in the first place is not cross-platform. Different hardware handles floats and doubles differently. What specifically are you concerned about?
Last edited on Aug 12, 2013 at 2:43am UTC
Aug 12, 2013 at 2:46am UTC
The thing is I'm using this for my 3D model format. I don't want my data to be read wrong.
Endians aren't a problem.
Aug 12, 2013 at 2:48am UTC
If endianess isn't an issue I don't see what you're worried about ;)
Aug 12, 2013 at 2:50am UTC
Oh, thanks :D But how would I read them?
Last edited on Aug 12, 2013 at 2:51am UTC