View previous topic :: View next topic |
Author |
Message |
flarn2006 Advanced Cheater Reputation: 1
Joined: 27 Nov 2012 Posts: 73
|
Posted: Fri Dec 28, 2012 9:59 pm Post subject: How do I convert a floating point number to bytes? |
|
|
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 |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25295 Location: The netherlands
|
Posted: Sat Dec 29, 2012 4:18 am Post subject: |
|
|
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 |
|
|
flarn2006 Advanced Cheater Reputation: 1
Joined: 27 Nov 2012 Posts: 73
|
Posted: Sat Dec 29, 2012 7:39 pm Post subject: |
|
|
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 |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25295 Location: The netherlands
|
Posted: Sun Dec 30, 2012 4:00 am Post subject: |
|
|
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 |
|
|
flarn2006 Advanced Cheater Reputation: 1
Joined: 27 Nov 2012 Posts: 73
|
Posted: Sun Dec 30, 2012 3:27 pm Post subject: |
|
|
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 |
|
|
|