Originally Posted by
Elysia
It's really quite a poor solution. The float's actual value would be undefined.
It's better to just use 4 chars to do this. Perhaps even use std::tr1::array to wrap them in a single container with no overhead.
But I still fail to understand what exactly it is that must be achieved, so this is the best solution I know of.
Why would it be undefined, if it is a union the float will have the same place in memory as the chars right? So starting from a float value I would assign the float value and in the char[4] that float value would be split up in 4 parts each part holding 0 - 255. Isn't this a really elegant solution?
So in my case I could do:
Code:
union {
float fv;
char sv[4];
} e;
float thickness = 40.86f;
e.fv = thickness;
Color col;
col.r = e.sv[0];
col.g = e.sv[1];
col.b = e.sv[2];
col.a = e.sv[3];
bitmap->putPixels(x,y,col);
and to convert back:
Code:
Color col = Color(12,64,255,0);
char ch[4] = {(char)col.r, (char)col.g, (char)col.b, (char)col.a};
e.sv = ch;
float thickness = e.fv;
Or could I?