Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How do I convert a floating point number to bytes?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
flarn2006
Advanced Cheater
Reputation: 1

Joined: 27 Nov 2012
Posts: 73

PostPosted: Fri Dec 28, 2012 9:59 pm    Post subject: How do I convert a floating point number to bytes? Reply with quote

So I'm writing a script that needs to store values to be written at a later time, but these values can be in either float or int format, as it's not always certain which type the game is expecting. Rather than use a variable to keep track of whether to use writeFloat or writeInteger, I'd like to just store the floating-point bytes in the variable as an integer that can be written using writeInteger. Without writing the value to memory using writeFloat and then reading it back using readInteger (since this needlessly uses four bytes of the game's memory) I'd like to do this entirely in the Lua script. What function can I use that will turn, say, 1, into 1,065,353,216 (0x3F800000)? Or 0.5 into 1,056,964,608 (0x3F000000)?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25288
Location: The netherlands

PostPosted: Sat Dec 29, 2012 4:18 am    Post subject: Reply with quote

writeFloat(address, 1) will write 0x3f800000 to the given address
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
flarn2006
Advanced Cheater
Reputation: 1

Joined: 27 Nov 2012
Posts: 73

PostPosted: Sat Dec 29, 2012 7:39 pm    Post subject: Reply with quote

Yes, I'm aware of that. What I'm asking is how to do that without writing it to an address first. In case I need to store the value in a Lua variable for later.

Something that does what this C function would do:
Code:
int encodeFloatToInt(float value)
{
    return *(int*)&value;
}
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25288
Location: The netherlands

PostPosted: Sun Dec 30, 2012 4:00 am    Post subject: Reply with quote

to lua there is no difference if it's an integer or float, it's always a double to lua

If you only need to store and write at a later time and don't have to look at the value in an interpreted way, then just use readInteger/writeInteger without any conversion, as integers can be written without loss of information

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
flarn2006
Advanced Cheater
Reputation: 1

Joined: 27 Nov 2012
Posts: 73

PostPosted: Sun Dec 30, 2012 3:27 pm    Post subject: Reply with quote

That's what I'd like to do, but first I have to encode the number as a float. Let's say I want to write a function that reads a string with a number in it and writes that number to memory. If the string begins with "0x", it will write it as an integer. Otherwise it will write it as a float. That's easy enough; just use writeInteger or writeFloat depending on whether or not it begins with "0x". But what if I don't want to write it immediately, but rather store it in a table of values to be written later? I could store a boolean value as well that says whether to use writeFloat or writeInteger, but it would make a lot more sense to just store the float value as a value that can be written using writeInteger, like writeInteger(address, 0x3F800000).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites