This is what I have, currently. Is there any nicer way to do this?
import struct
def int32_to_uint32(i):
return struct.unpack_from("I", struct.pack("i", i))[0]
Not sure if it's "nicer" or not...
import ctypes
def int32_to_uint32(i):
return ctypes.c_uint32(i).value
1 and -1, the OP's version returns an int and a long respectively. My version returns a long for both.struct?int for both cases. Also, what's the basis of your preference? You've never said what sort of nicer you're after.I just started learning python, but something simple like this works for values in the range of a signed 32-bit integer
def uint(x):
if x < 0:
return hex(0xffff_ffff - abs(x) + 1)
else:
return hex(x)