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

北慕城南 提交于 2019-12-30 06:11:28

问题


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]

回答1:


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

import ctypes

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



回答2:


using numpy for example:

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

or even on arrays,

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


来源:https://stackoverflow.com/questions/16452232/how-to-convert-signed-32-bit-int-to-unsigned-32-bit-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!