Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 241
Claudiu Avatar asked May 09 '13 00:05

Claudiu


1 Answers

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

import ctypes

def int32_to_uint32(i):
    return ctypes.c_uint32(i).value
like image 121
martineau Avatar answered Oct 05 '22 22:10

martineau