Python - 十进制到十六进制、反转字节顺序、十六进制到十进制

发布于 2024-11-06 18:13:04 字数 389 浏览 4 评论 0原文

我已经阅读了很多关于 Stuct.pack 和 hex 等的内容。

我正在尝试将十进制转换为 2 字节的十六进制。反转十六进制位顺序,然后将其转换回十进制。

我正在尝试按照这些步骤......在 python 中

Convert the decimal value **36895** to the equivalent 2-byte hexadecimal value:

**0x901F**
Reverse the order of the 2 hexadecimal bytes:

**0x1F90**
Convert the resulting 2-byte hexadecimal value to its decimal equivalent:

**8080**

I've been reading up a lot on stuct.pack and hex and the like.

I am trying to convert a decimal to hexidecimal with 2-bytes. Reverse the hex bit order, then convert it back into decimal.

I'm trying to follow these steps...in python

Convert the decimal value **36895** to the equivalent 2-byte hexadecimal value:

**0x901F**
Reverse the order of the 2 hexadecimal bytes:

**0x1F90**
Convert the resulting 2-byte hexadecimal value to its decimal equivalent:

**8080**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风柔一江水 2024-11-13 18:13:04

移位以交换高/低八位:

>>> x = 36895
>>> ((x << 8) | (x >> 8)) & 0xFFFF
8080

以相反的字节顺序(<>)打包和解包无符号短(H):

>>> struct.unpack('<H',struct.pack('>H',x))[0]
8080

将2字节小字节序转换为大字节序...

>>> int.from_bytes(x.to_bytes(2,'little'),'big')
8080

Bit shifting to swap upper/lower eight bits:

>>> x = 36895
>>> ((x << 8) | (x >> 8)) & 0xFFFF
8080

Packing and unpacking unsigned short(H) with opposite endianness(<>):

>>> struct.unpack('<H',struct.pack('>H',x))[0]
8080

Convert 2-byte little-endian to big-endian...

>>> int.from_bytes(x.to_bytes(2,'little'),'big')
8080
懷念過去 2024-11-13 18:13:04

请记住,“十六进制”(以 16 为基数 0-9 和 af)和“十进制”(0-9) 只是人类用来表示数字的构造。这些都是机器的部分。

python hex(int) 函数生成一个十六进制 'string' 。如果你想将其转换回十进制:

>>> x = 36895
>>> s = hex(x)
>>> s
'0x901f'
>>> int(s, 16)  # interpret s as a base-16 number

Keep in mind that 'hex'(base 16 0-9 and a-f) and 'decimal'(0-9) are just constructs for humans to represent numbers. It's all bits to the machine.

The python hex(int) function produces a hex 'string' . If you want to convert it back to decimal:

>>> x = 36895
>>> s = hex(x)
>>> s
'0x901f'
>>> int(s, 16)  # interpret s as a base-16 number
╭⌒浅淡时光〆 2024-11-13 18:13:04

打印格式也适用于字符串。

# Get the hex digits, without the leading '0x'
hex_str = '%04X' % (36895)

# Reverse the bytes using string slices.
# hex_str[2:4] is, oddly, characters 2 to 3.
# hex_str[0:2] is characters 0 to 1.
str_to_convert = hex_str[2:4] + hex_str[0:2]

# Read back the number in base 16 (hex)
reversed = int(str_to_convert, 16)

print(reversed) # 8080!

Print formatting also works with strings.

# Get the hex digits, without the leading '0x'
hex_str = '%04X' % (36895)

# Reverse the bytes using string slices.
# hex_str[2:4] is, oddly, characters 2 to 3.
# hex_str[0:2] is characters 0 to 1.
str_to_convert = hex_str[2:4] + hex_str[0:2]

# Read back the number in base 16 (hex)
reversed = int(str_to_convert, 16)

print(reversed) # 8080!
挖个坑埋了你 2024-11-13 18:13:04

我的方法


import binascii

n = 36895
reversed_hex = format(n, 'x').decode('hex')[::-1]
h = binascii.hexlify(reversed_hex)
print int(h, 16)

或者一行

print int(hex(36895)[2:].decode('hex')[::-1].encode('hex'), 16)
print int(format(36895, 'x').decode('hex')[::-1].encode('hex'), 16)
print int(binascii.hexlify(format(36895, 'x').decode('hex')[::-1]), 16)

或者使用 bytearray

import binascii

n = 36895
b = bytearray.fromhex(format(n, 'x'))
b.reverse()
print int(binascii.hexlify(b), 16)

My approach


import binascii

n = 36895
reversed_hex = format(n, 'x').decode('hex')[::-1]
h = binascii.hexlify(reversed_hex)
print int(h, 16)

or one line

print int(hex(36895)[2:].decode('hex')[::-1].encode('hex'), 16)
print int(format(36895, 'x').decode('hex')[::-1].encode('hex'), 16)
print int(binascii.hexlify(format(36895, 'x').decode('hex')[::-1]), 16)

or with bytearray

import binascii

n = 36895
b = bytearray.fromhex(format(n, 'x'))
b.reverse()
print int(binascii.hexlify(b), 16)
宛菡 2024-11-13 18:13:04

要将十进制转换为十六进制,请使用:

dec = 255
print hex(dec)[2:-1]

这将输出 255 的十六进制值。
要转换回十进制,请使用

hex = 1F90
print int(hex, 16)

这将输出 1F90 的十进制值。

您应该能够使用以下命令反转字节:

hex = "901F"
hexbyte1 = hex[0] + hex[1]
hexbyte2 = hex[2] + hex[3]
newhex = hexbyte2 + hexbyte1
print newhex

这将输出 1F90。希望这有帮助!

To convert from decimal to hex, use:

dec = 255
print hex(dec)[2:-1]

That will output the hex value for 255.
To convert back to decimal, use

hex = 1F90
print int(hex, 16)

That would output the decimal value for 1F90.

You should be able to reverse the bytes using:

hex = "901F"
hexbyte1 = hex[0] + hex[1]
hexbyte2 = hex[2] + hex[3]
newhex = hexbyte2 + hexbyte1
print newhex

and this would output 1F90. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文