Jump to content

[SA CLEO] How to convert a string to float?


zhawk43

Recommended Posts

I'm making a script that reads a lot of strings of a .txt file, i know that the function 0x82258E can convert a string to int but it doesn't work with floats. Is there a function that does that or a workaround for it?

Link to comment
Share on other sites

10 hours ago, Jack said:

@zhawk43 Have you tried the opcode:

008F: 1@ = integer 0@ to_float

?

@Jack, thanks a lot for responding it! but i kinda reached out a solution not so different from using this 008F opcode

What i was doing is recording a car coordinates frame by frame into a txt file using the 0AD9 opcode, literally writing the float as something like -20.525856, and to load everything again into the game i've used the 0AD7 opcode in conjunction with the 0x8529294 function to convert that string into a float, and i was obviously receiving just a -20, which is useless. After some time i remembered that i could just write the float values as ints in the txt file, instead of -20.52586 to -11102356, and now using again the 0x8529294 function into that string i finally received my float with the correct value of -20.52486. Yeah, noob problems, but i'm really happy that this thing is working now, Thanks again, man!

Link to comment
Share on other sites

What about atof (0x00823CEE) routine? Call it like a void function and use POP_FLOAT (2693) command to get the return value.

Link to comment
Share on other sites

On 6/9/2023 at 3:03 PM, Wesser said:

What about atof (0x00823CEE) routine? Call it like a void function and use POP_FLOAT (2693) command to get the return value.

Sorry for the ignorance, but how exactly i should use that in cleo? I really have no idea.

Link to comment
Share on other sites

Something as simple as follows:

 

atoftest.txt

{$CLEO}

const
    _atof = 0@ // double __cdecl atof(const char *str)
end
0A8D: read_memory 0x005B0EB1 size 4 vp FALSE store_to _atof
_atof += 0x005B0EB5

0AC6: get_label_pointer @_fltval store_to 1@

0AA5: call_function_no_result _atof push 1 pop 1 str 1@
0AE9: pop_float 2@

2@ *= 1000.0
0092: cset 3@ 2@

03F0: use_text_commands TRUE
while true
    wait 0
    0342: set_text_center TRUE
    045A: display_text_with_number 320.0 300.0 tkey 'NUMBER' number 3@ // ~1~
end

:_fltval
hex
    "123.456" 00
end
Link to comment
Share on other sites

Why not just use 0AD4?

 

if 
	0AD4: 4@ = scan_string 0@v format "%f" 5@
then
	// 5@ is now float
end

This function also allows you to parse multiple values at once, accepting some input string format.

 

Why are you writing data into file as text? Have it to be human readable? It sound like you should just write and read binary data without any conversions.

Edited by MiranDMC
Link to comment
Share on other sites

  • 10 months later...

alloc 10@ = 128
0AD3: string 10@ format "1.5 58 0x840F" //using as strings all value
0AD4: 4@ = scan_string 10@ format "%f %i %x" 1@ 2@ 3@  //here we wanna get values out from strings
if    4@ > 0 //0AD4: this 4@ (first parameters)is its like counting functions as you can see i use 3 function float and int and hex 
then  0ad1: "%f %i 0x%x ~n~Functions %i" 2000 1@ 2@ 3@ 4@
else  0ad1: "~r~Error" 2000
end

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.