I have a program that should take 4 bytes and convert them to an IEEE-754 float. Bytes are transmitted out of order, but I can return them in order, just fine. My problem is to pull them into the float. Relevant parts of the code:
//Union to store bytes and float on top of each other typedef union { unsigned char b[4]; float f; } bfloat; //Create instance of the union bfloat Temperature; //Add float data using transmitted bytes MMI.Temperature.b[2] = 0xD1;//MMIResponseMsg[7]; MMI.Temperature.b[3] = 0xE1;//MMIResponseMsg[8]; MMI.Temperature.b[0] = 0x41;//MMIResponseMsg[9]; MMI.Temperature.b[1] = 0xD7;//MMIResponseMsg[10]; //Attempting to read the float value lWhole=(long) ((float)MMI.Temperature.f); //DEBUGGING stevenFloat = (float)MMI.Temperature.f;
lWhole is long, and stevenFloat is a float. When debugging, I see that the values ββassigned to the byte array are stored correctly, however, the values ββof stevenFloat and lWhole are incorrect. They seem to be approaching 0 or close to max float / long values. Long and float are both 32 bits with my compiler.
Does anyone know why this is not working? It looked right to me when I got the code to work, and it seems to be a common solution on the Internet, I'm just at a dead end.
c ++ c casting
Steven
source share