Sure. You can certainly construct a floating point number from a raw byte buffer. Single precision float
s are represented as 32-bit values in IEEE-754 and can represent the integer space quite easily. The wikipedia entry on IEEE floating point describes the various formats. All that you have to do is figure out which bits to set or test. Look at the wikipedia page on single-precision for a nice description of the bits. Here's a start:
float convert_int(unsigned int num) {
float result;
unsigned int sign_bit = (num & 0x80000000);
unsigned int exponent = (num & 0x7F800000) >> 23;
unsigned int mantissa = (num & /* insert mask value here */);
/* you can take over from here */
return result;
}
I left a lot up to the imagination, but I have a feeling that this is a homework problem. Once you understand the part that I have written, it should be easy enough to do the rest. A much nicer solution is to use a union
, but I will leave that as an exercise to the reader as well.