Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 反整数为十六进制_Python_Python 3.x - Fatal编程技术网

Python 反整数为十六进制

Python 反整数为十六进制,python,python-3.x,Python,Python 3.x,我在python中有int,我想反转它 x=int(1234567899) 我想要的结果将是3674379849 解释:=1234567899=0x499602DB和3674379849=0xDB029649 如何在python中实现这一点?>import struct >>> import struct >>> struct.unpack('>I', struct.pack('<I', 1234567899))[0] 3674379849 >

我在python中有int,我想反转它

x=int(1234567899)
我想要的结果将是
3674379849

解释:=
1234567899
=
0x499602DB
3674379849
=
0xDB029649

如何在python中实现这一点?

>import struct
>>> import struct
>>> struct.unpack('>I', struct.pack('<I', 1234567899))[0]
3674379849
>>>

>>>struct.unpack('>I',struct.pack('如果您只是想要结果,请使用-如果您想要炫耀权限的中间步骤,您需要

  • 创建数字(#1)的十六进制,并可能添加前导0以确保正确性
  • 按2字节顺序将其反转(#2)
  • 再次创建一个整数(#3)
f、 像这样

n = 1234567899
# 1
h = hex(n)              
if len(h) % 2:    # fix for uneven lengthy inputs (f.e. n = int("234",16))
    h = '0x0'+h[2:]
# 2 (skips 0x and prepends 0x for looks only)
bh = '0x'+''.join([h[i: i+2] for i in range(2, len(h), 2)][::-1])
# 3
b = int(bh, 16)
print(n, h, bh, b)
得到

1234567899 0x499602db 0xdb029649 3674379849

将int转换为十六进制字符串(提示:python具有内置函数
hex
),反转字符串,再次将字符串转换为int。看起来您正在尝试从小端表示转换为大端表示,反之亦然。我是否正确?在这种情况下,您可能需要struct.pack和struct.unpack。这是否回答了您的问题?我不理解这个问题。如果我需要阅读可能的答案来猜测实际问题n、 我觉得这个问题本身需要更多的细节/解释。