javamemoryunsafememory-addressdword

Memory values with java


I've just wrote some code, which accesses the memory. I checked the address (in code) with CheatEngine, and I printed it with System.out, and it's different. I know it's a long value, but in hex, the value 2 is 00000002 (which was the result in CheatEngine), and I get 844287491178496 with java. Why is that? How can I convert the value from the address to an int? And more important, what is the long value I've just read from the memory with java? Here's my code:

import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class Memory2 {
    public static void main(String args[]){
        Unsafe unsafe = getUnsafe();
        long address = 0x002005C;
    unsafe.getAddress(address);
    System.out.println(unsafe.getAddress(address));
}

public static Unsafe getUnsafe() {
try {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
        return (Unsafe)f.get(null);
    } catch (Exception e) {}
        return null;
}
}

Solution

  • A long in java is 8 bytes, not a dword; your value from CheatEngine appears to be four bytes. I think the difference might be just that. 0000000200000000 hex is 8589934592; the bytes beyond the four you are checking with CheatEngine might explain the value Java is showing.