Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Floating Point to Character Array?

Status
Not open for further replies.

dougcoulter

Programmer
Mar 16, 2001
112
US
I have a real problem that is driving me crazy!

I have a floating point (4 bytes) variable that I need to convert into a 4 byte character array. I just want the raw data that makes up the 4 bytes of the float to be directly placed in the character array (no conversion or anything).

For example, if the floating point variable contains the number 22.625 ( = 0100 0001 1011 0101 0000 0000 0000 0000 ), I need a character array to be populated as

ch[0] = 0x41h = 0100 0001b
ch[1] = 0xB5h = 1011 0101b
ch[2] = 0x00h = 0000 0000b
ch[3] = 0x00h = 0000 0000b

Can anyone help me with the code required to accomplish this?

Thank you so much...

...Doug Coulter
 
memcpy will do the trick. The hard part is proving that it works. The following code provides the verification code as well.


char ch[4];
float f = 22.625;
memcpy( ch, &f, sizeof(f));

// now prove that 'ch' has the value by copying it into 'f2'
float f2 = 0.00;
memcpy( &f2, ch, sizeof(f));

TRACE("%f, %f\r\n", f, f2 );


Hope this helps
-pete
 
That is exactly what I needed - thank you so much!

As pathetic as it seems, I have been trying to solve that problem for more than a day now! Based on the MSDN examples for memcpy, I thought it was only for use with character data types.

Thanks again!

Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top