如何从python中的地址获取价值

时间:2018-09-28 11:00:21

标签: python

我正在尝试从Python中的地址读取值。

假设我在变量中有一个地址:address_vble= 0x900045A1

在C语言中,我们只能得到value = *address_vble

如何在Python中做同样的事情?

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您可以按ctypes查找它

>>>import ctypes
>>>a = 5
>>>address = id(a)
>>>address
493382800
>>>ctypes.cast(address, ctypes.py_object).value
5

希望它将对您有帮助!

答案 1 :(得分:1)

通过存储器地址访问值

示例1

import ctypes
sn = 1 # Value to store on sn
value1 = sn # Value of sn
print("value1 =",value1)
memory_address = id(sn) # Get address of sn variable
value2 = ctypes.cast(memory_address, ctypes.py_object).value #Get value from  address of sn variable
print("value2 =",value2)

示例2

import ctypes
value3 = 100
memory_address2=id(value3) # Getting address of variable
print("value 3 =", value3)
print("Memory Address (INT) =",memory_address2)
memory_address3=hex(memory_address2) # Integer to hex conversion
print("Memory Address (HEX) =",memory_address3)
memory_address4=int(memory_address3, base=16) # Revert to Integer Type from hex
print("Memory Address (INT) =",memory_address4)
value4 = ctypes.cast(memory_address4, ctypes.py_object).value # Getting value of address
print("value 4 =", value4)

答案 2 :(得分:0)

@PrashuPratik我们可以将十六进制地址转换为整数,然后可以检查任何内存地址的值。

import ctypes
    x=id(a)
    x
    493382800
    y=hex(x)
    y
   '0xc545d0'
    z=int(y,base=16)  #this will convert the hexadecimal value into integer value
    ctypes.cast(x,ctypes.py_object).value
    5
  

int('hexadecimal',base=16)使用此功能可以转换任何   将十六进制数转换为整数。

我希望这能解决您的问题