2012-12-04 29 views
2

我剛剛寫了一段代碼,訪問內存,我用cheatengine檢查了地址(在代碼中),並且我用System.out打印了它,這是不同的。我知道這是一個很長的值,但是在十六進制中,值2是00000002(這是CheatEengine的結果),並且我用java獲得了844287491178496。那爲什麼呢?我如何將地址的值轉換爲int?更重要的是,用java來記憶我的紅色長久價值是什麼?繼承人是我的代碼:使用java的內存值

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; 
} 
} 
+1

不是Java沙箱這個東西,所以它決定東西去哪裏,而不是讓你寫任何你想要的地方?看起來這是它的一個特點,爲了避免處理這類東西而來到地球上的地獄。 – RonaldBarzell

+1

@ user1161318通常情況是這樣,但這是'Unsafe'類,它顧名思義就是圍繞這一切。 – biziclop

+0

我懷疑這可能在它後面:'從給定的內存地址獲取本機指針。如果地址爲零,或者沒有指向從#allocateMemory獲得的塊,則結果是不確定的。「 – biziclop

回答

2

長在Java是8個字節,一個DWORD; CheatEngine的值看起來是四個字節。我認爲差異可能就在於此。 0000000200000000十六進制是8589934592;您使用CheatEngine檢查的四個字節之外的字節可能會解釋Java顯示的值。

+0

如此真實,這一定是答案 –

0

從源爲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); 

總之它將在32位JVM和64位上的64位JVM讀取32位(爲無符號值)。

注意:大多數64位JVM對於典型堆大小將使用32位引用。

+0

好吧我要去照顧這些值,也許CE是一個32位版本,我有64位操作系統 –