Return to Snippet

Revision: 8701
at October 3, 2008 11:09 by pckujawa


Initial Code
float aFloat = anotherFloat; // Could be a double instead
unsigned char bytes[sizeof(float)];
unsigned char *byte = (unsigned char *) &aFloat;

if(isBigEndian)
{
    for (i=0; i<sizeof(float); i++)
    {
        bytes[i] = *byte;
        byte++;
    }
}
else
{
    for (i=sizeof(float)-1; i>=0; i--)
    {
        bytes[i] = *byte;
        byte++;
    }
}

Initial URL

                                

Initial Description
This routine can be used to copy the byte values of a float or double. The "isBigEndian" variable needs to be determined (most PCs are little-endian, whereas most embedded microprocessors are big-endian). Instead of using unsigned chars, it can be useful to do a "typedef unsigned char UINT8" and simply use UINT8 as the variable type.

Initial Title
Obtaining the byte values in a float or double.

Initial Tags
c

Initial Language
C