How to get the signed integer value of a long in python?

Paul Oyster picture Paul Oyster · Sep 3, 2009 · Viewed 52.1k times · Source

If lv stores a long value, and the machine is 32 bits, the following code:

iv = int(lv & 0xffffffff)

results an iv of type long, instead of the machine's int.

How can I get the (signed) int value in this case?

Answer

Raony Barrios picture Raony Barrios · Feb 19, 2012
import ctypes

number = lv & 0xFFFFFFFF

signed_number = ctypes.c_long(number).value