float somef;
int temp;
somef = value;
temp = (int)somef;
Im not sure about that, but i think that it will put only integer part of float number into temp ... and that is not what we wanted.
I would do it like this:
float SwapFloat( float fin )
{
union {
float f;
unsigned char c[4];
} normal, swapped;
normal.f = fin;
swapped.c[0] = normal.c[3];
swapped.c[1] = normal.c[2];
swapped.c[2] = normal.c[1];
swapped.c[3] = normal.c[0];
return swapped.f;
}
Union is a good thingie, because all variables in it occupy same place. so c[0] is first byte of f, c[1] is 2nd ...
Or here is a more general function that i use, it is good for reversing anything (to 64bytes in length):
#define MaxSwapBytes 64
void Swap( void* data, int LengthInBytes )
{
assert( ((LengthInBytes > 0 ) && (LengthInBytes <= MaxSwapBytes )) );
if ( ! ((LengthInBytes > 0 ) && (LengthInBytes <= MaxSwapBytes )) )
return;
int i;
unsigned char *normal, swapped[ MaxSwapBytes ];
normal = (unsigned char*) data;
for( i=0; i<LengthInBytes; i++ )
swapped[ i ] = normal[ LengthInBytes - 1 - i ];
memcpy( data, swapped, LengthInBytes );
}
you have to supply a pointer to your variable and length in bytes, like swap( &f, sizeof( float ) ); or swap( &myshort, 2 );
It does the same as the one above, but in a cycle.