How to convert signed 32-bit int to unsigned 32-bit int?

Claudiu picture Claudiu · May 9, 2013 · Viewed 30.3k times · Source

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]

Answer

martineau picture martineau · May 9, 2013

Not sure if it's "nicer" or not...

import ctypes

def int32_to_uint32(i):
    return ctypes.c_uint32(i).value