Search code examples
pythoncheat-engine

ReadWriteMemory reading memory as an int instead of a float


from ReadWriteMemory import ReadWriteMemory

rwm = ReadWriteMemory()

process = rwm.get_process_by_name("javaw.exe")
process.open()
module_base = 0x6FBB0000
static_address_offset = 0x007FD7C0
static_address = module_base + static_address_offset
pitch_pointer = process.get_pointer(static_address, offsets=[0xB8, 0x1C8, 0x1C8, 0x1D0, 0x178, 0xAC, 0x8C])
camera_pitch = process.read(pitch_pointer)
print(camera_pitch)

I am trying to get the camera pitch using a pointer I got in cheat engine, and the script works fine but the camera pitch is a float value, while process.read(pitch_pointer) returns an int, and that for example sets camera_pitch to 1108138163 instead of 35.2. Can't find how to get a float instead anywhere.


Solution

  • You can use the struct module.

    Something like this:

    >>> import struct
    >>> i_value = 1108138163
    >>> struct.unpack("@f", struct.pack("@I", i_value))[0]
    35.21162033081055
    

    That is, you convert your integer to a 4-byte array, and then you convert that to a float. struct.unpack always returns a tuple, in this case of a single value, so use [0] to get to it.