如何转换符号32位INT无符号32位int?(How to convert signed 32-bi

2019-09-02 08:07发布

这是我目前。 有没有更好的方式来做到这一点?

import struct
def int32_to_uint32(i):
    return struct.unpack_from("I", struct.pack("i", i))[0]

Answer 1:

不知道是否是“更好”或不...

import ctypes

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


Answer 2:

使用numpy的例如:

import numpy
result = numpy.uint32( numpy.int32(myval) )

或甚至在阵列

arr = numpy.array(range(10))
result = numpy.uint32( numpy.int32(arr) )


文章来源: How to convert signed 32-bit int to unsigned 32-bit int?