Convert string to float ?

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

shihchiy

New Member
Feb 1, 2005
15
0
0
Hi,

If I receive a string which includes 4 characters (Total 32 bits). How can I convert it to a float (32 bits).

Best

Luke
 

Mychaeel

New Member
Angel_Mapper said:
Just doing a float(String) should work.
No -- that'd only work if the string was an actual decimal character representation of the number (e.g. "3.14"). As I understand it, the string contains a binary representation of a floating-point number (e.g. the byte values 0xC3 0xF5 0x48 0x40).

shihchiy: If my assumption is right, you'll probably have to use the Asc(...) function to get the individual byte values of that string and construct the resulting float number yourself. There's no built-in function for that conversion.
 

shihchiy

New Member
Feb 1, 2005
15
0
0
Yes, your assumption is correct.
If I encode a float(32 bits) into a 4-char string (32 bits) in C++. And send the string to my Unrealscript program through TCPlink. How can I decode it using Asc(..) ?

Best

Luke

Mychaeel said:
No -- that'd only work if the string was an actual decimal character representation of the number (e.g. "3.14"). As I understand it, the string contains a binary representation of a floating-point number (e.g. the byte values 0xC3 0xF5 0x48 0x40).

shihchiy: If my assumption is right, you'll probably have to use the Asc(...) function to get the individual byte values of that string and construct the resulting float number yourself. There's no built-in function for that conversion.
 

elmuerte

Master of Science
Jan 25, 2000
1,936
0
36
42
the Netherlands
elmuerte.com
the question is, how to encode a float into 4 bytes, what float format are you using.

with Asc() you can convert an ASCII char to it's integer representation, using that you can convert the four chars to 4 bytes. After that you only have to convert the 4 bytes to a float, this depends on your float format.
 

shihchiy

New Member
Feb 1, 2005
15
0
0
The pseudo code in c++ to store a float into a string is as below

float f;
char buf[20];
memcpy((void *)buf, &f,sizeof(f));

buf is sent to Unrealscript through TcpLink.

Any idea about how to convert buf back to float in Unrealscript program ?

Best

Luke

Mychaeel said:
Wikipedia: IEEE Float should have all information and links you need.
 

Mychaeel

New Member
Well... understand how the IEEE float format works (see link) and convert the bytes to a float yourself. You can get the bytes using the Asc() function.

I'm afraid I can't think of any better way to do it, other than changing the protocol you're using -- you might want to transfer that numerical value in decimal form (13.37 as "1" "3" "." "3" "7") or use a fixed-point number (13.37 as 13.37 << 16 == 876216 which translates back to 13.3699951...).