Search code examples
pythonintegerunsignedsignedtype-conversion

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


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]

Solution

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

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