gpt4 book ai didi

python - 如何在Python中将整数转换为无符号32位?

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:38 24 4
gpt4 key购买 nike

例如,如果我们将1转换为无符号32位,则它将是00000000000000000000000000000001

如何在 python 中实现这一目标?

最佳答案

def convert(i):
return int(bin(i+2**32)[-32:])

print convert(1)
print convert(32)
print convert(100)
print convert(-1)
print type(convert(1))

输出

00000000000000000000000000000001
00000000000000000000000000100000
00000000000000000000000001100100
11111111111111111111111111111111
<type 'long'>

关于python - 如何在Python中将整数转换为无符号32位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28495393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com