0

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).

3
  • I think you mean raw "byte" string, not "binary" Commented May 17, 2013 at 19:43
  • 2
    The %x format won't print the float because the call to printf() automatically converts the float to a double (and anything shorter than int to int). What happens when you use %e instead of %f? Commented May 17, 2013 at 19:46
  • I get: recBuffer=0x12345678 tempFloat=*************************************** (1.737824e+034) Amazing. It's never where you'd expect. Thank you so much. Commented May 17, 2013 at 20:04

2 Answers 2

1
printf( "tempFloat=%d (0x%x)\n", tempFloat, tempFloat); ^ | | | +---------------------+ 

The %x specifier is useful for integer numbers but you're passing a float value to the printf. So the output is not a meaningful value.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it! recBuffer=0x12345678 tempFloat=*************************************** (1.737824e+034) I still don't understand why %f would return ****, but I'm happy memcpy() does what I'd expect. Thank you for your help!
0
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, *(unsigned*)&tempFloat ); return 0; } /* recBuffer=0x12345678 tempFloat=17378244361449504000000000000000000.000000 (0x78563412) */ 

1 Comment

%f still gives me *******, but using your advice: printf( "tempFloat=%e (0x%x)\n", tempFloat, *(unsigned*)&tempFloat ); allows me to get the raw hex value!: recBuffer=0x12345678 tempFloat=1.737824e+034 (0x78563412). That helps me alot, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.