I am trying to read the value from an address in Python.
Suppose I have an address in variable: address_vble= 0x900045A1
In C, we can just get value = *address_vble
How can I do the same in Python?
Thanks for assistance.
I am trying to read the value from an address in Python.
Suppose I have an address in variable: address_vble= 0x900045A1
In C, we can just get value = *address_vble
How can I do the same in Python?
Thanks for assistance.
You can find it by ctypes
>>>import ctypes
>>>a = 5
>>>address = id(a)
>>>address
493382800
>>>ctypes.cast(address, ctypes.py_object).value
5
Hope it will help you!
@PrashuPratik we can convert hexa-decimal address into integer and then we can check the value at any memory address.
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)
by using this you can convert any hexadecimal number into integer.
I hope this will resolve your problem
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)
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)