Question

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;
}
}
Was it helpful?

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.

OTHER TIPS

From the source for sun.misc.Unsafe

/**
 * Fetches a native pointer from a given memory address.  If the address is
 * zero, or does not point into a block obtained from {@link
 * #allocateMemory}, the results are undefined.
 *
 * <p> If the native pointer is less than 64 bits wide, it is extended as
 * an unsigned number to a Java long.  The pointer may be indexed by any
 * given byte offset, simply by adding that offset (as a simple integer) to
 * the long representing the pointer.  The number of bytes actually read
 * from the target address maybe determined by consulting {@link
 * #addressSize}.
 *
 * @see #allocateMemory
 */
 public native long getAddress(long address);

In short it will read 32-bit (as an unsigned value) on a 32-bit JVM and 64-bits on a 64-bit JVM.

Note: Most 64-bit JVMs will use 32-bit references for typical heap sizes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top