I'm trying to assign a float from a raw binary string, but I don't get what I would expect.
int main()
{
char recBuffer[] = {0x44, 0x42, 0x96, 0x12, 0x34, 0x56, 0x78};
float tempFloat;
int position = 3;
printf( "recBuffer=0x%x%x%x%x\n", recBuffer[3], recBuffer[4], recBuffer[5], recBuffer[6]);
memcpy( &tempFloat, recBuffer + position, 4 );
printf( "tempFloat=%f (0x%x)\n", tempFloat, tempFloat );
return 0;
}
My output looks like:
recBuffer=0x12345678
tempFloat=*************************************** (0x40000000)
The above procedure works for integers:
int main()
{
char recBuffer[] = {0x44, 0x42, 0x96, 0x12, 0x34, 0x56, 0x78};
int tempFloat;
int position = 3;
printf( "recBuffer=0x%x%x%x%x\n", recBuffer[3], recBuffer[4], recBuffer[5], recBuffer[6]);
memcpy( &tempFloat, recBuffer + position, 4 );
printf( "tempFloat=%d (0x%x)\n", tempFloat, tempFloat );
return 0;
}
with an output:
recBuffer=0x12345678
tempFloat=2018915346 (0x78563412)
(I'm aware of Endianness.)
I've tried to assign the float directly, but I still get something weird as an output (what do all the *'s mean?).
int main()
{
char recBuffer[] = {0x44, 0x42, 0x96, 0x12, 0x34, 0x56, 0x78};
float* tempFloat;
int position = 3;
printf( "recBuffer=0x%x%x%x%x\n", recBuffer[3], recBuffer[4], recBuffer[5], recBuffer[6]);
tempFloat = (float*)(recBuffer + position);
printf( "tempFloat=%f (0x%x)\n", *tempFloat, *tempFloat );
return 0;
}
with output:
recBuffer=0x12345678
tempFloat=*************************************** (0x40000000)
Any binary sequence should give me an output, as 0x78563412 = 1.73782443614495040019632524267E34. I'm not sure why 0x40000000 = *. It should be 2.0E0. What am I doing wrong?! Any help would be appreciated!
(unfortunately I'm working on an old QNX machine, with no debugger. Just simple printf()'s to help me along).
%xformat won't print thefloatbecause the call toprintf()automatically converts thefloatto adouble(and anything shorter thaninttoint). What happens when you use%einstead of%f?recBuffer=0x12345678 tempFloat=*************************************** (1.737824e+034)Amazing. It's never where you'd expect. Thank you so much.