从十六进制转换为二进制而不丢失前导 0 python

发布于 2024-09-10 09:22:28 字数 224 浏览 8 评论 0原文

我在字符串中有一个十六进制值,

h = '00112233aabbccddee'

我知道我可以将其转换为二进制:

h = bin(int(h, 16))[2:]

但是,这会丢失前导 0。有没有办法在不丢失 0 的情况下进行这种转换?或者最好的方法是在转换之前计算前导 0 的数量,然后将其添加进去。

I have a hex value in a string like

h = '00112233aabbccddee'

I know I can convert this to binary with:

h = bin(int(h, 16))[2:]

However, this loses the leading 0's. Is there anyway to do this conversion without losing the 0's? Or is the best way to do this just to count the number of leading 0's before the conversion then add it in afterwards.

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

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

发布评论

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

评论(7

说不完的你爱 2024-09-17 09:22:28

我认为没有办法默认保留这些前导零。

每个十六进制数字转换为 4 个二进制数字,因此新字符串的长度应该恰好是原始字符串大小的 4 倍。

h_size = len(h) * 4

然后,您可以使用 .zfill 将零填充到您想要的大小:

h = ( bin(int(h, 16))[2:] ).zfill(h_size)

I don't think there is a way to keep those leading zeros by default.

Each hex digit translates to 4 binary digits, so the length of the new string should be exactly 4 times the size of the original.

h_size = len(h) * 4

Then, you can use .zfill to fill in zeros to the size you want:

h = ( bin(int(h, 16))[2:] ).zfill(h_size)
罪歌 2024-09-17 09:22:28

这在 Python 中实际上很容易,因为它对整数的大小没有任何限制。只需在十六进制字符串前面添加一个 '1',然后从输出中去除相应的 '1' 即可。

>>> h = '00112233aabbccddee'
>>> bin(int(h, 16))[2:]  # old way
'1000100100010001100111010101010111011110011001101110111101110'
>>> bin(int('1'+h, 16))[3:]  # new way
'000000000001000100100010001100111010101010111011110011001101110111101110'

This is actually quite easy in Python, since it doesn't have any limit on the size of integers. Simply prepend a '1' to the hex string, and strip the corresponding '1' from the output.

>>> h = '00112233aabbccddee'
>>> bin(int(h, 16))[2:]  # old way
'1000100100010001100111010101010111011110011001101110111101110'
>>> bin(int('1'+h, 16))[3:]  # new way
'000000000001000100100010001100111010101010111011110011001101110111101110'
沫雨熙 2024-09-17 09:22:28

基本相同,但每个十六进制填充 4 个二进制数字

''.join(bin(int(c, 16))[2:].zfill(4) for c in h)

Basically the same but padding to 4 bindigits each hexdigit

''.join(bin(int(c, 16))[2:].zfill(4) for c in h)
晨曦慕雪 2024-09-17 09:22:28

当 python 将字符串转换为十六进制时,像我这样的 python 新手会像这样进行

datastring = 'HexInFormOfString'  

填充以容纳前面的零(如果有)。

datastrPadded = 'ffff' + datastring 

将填充值转换为二进制。

databin = bin(int(datastrPadded,16))

删除 python 添加的 2 位('0b')以表示二进制 + 16 填充位。

databinCrop = databin[18:]

A newbie to python such as I would proceed like so

datastring = 'HexInFormOfString'  

Padding to accommodate preceding zeros if any, when python converts string to Hex.

datastrPadded = 'ffff' + datastring 

Convert padded value to binary.

databin = bin(int(datastrPadded,16))

Remove 2bits ('0b') that python adds to denote binary + 16 padded bits .

databinCrop = databin[18:]
盗琴音 2024-09-17 09:22:28

这会将十六进制字符串转换为二进制字符串。由于您希望长度取决于原始内容,因此这可能就是您想要的。

data = ""
while len(h) > 0:
    data = data + chr(int(h[0:2], 16))
    h = h[2:]
print h

This converts a hex string into a binary string. Since you want the length to be dependent on the original, this may be what you want.

data = ""
while len(h) > 0:
    data = data + chr(int(h[0:2], 16))
    h = h[2:]
print h
萌面超妹 2024-09-17 09:22:28

我需要整数作为输入和带有前缀“0b”和“0x”的纯十六进制/二进制字符串,所以我的一般解决方案是这样的:

def pure_bin(data, no_of_bits=NO_OF_BITS):
    data = data + 2**(no_of_bits)
    return bin(data)[3:]

def pure_hex(data, no_of_bits=NO_OF_BITS):
    if (no_of_bits%4) != 0:
        no_of_bits = 4*int(no_of_bits / 4) + 4
    data = data + 2**(no_of_bits)
    return hex(data)[3:]

I needed integer as input and pure hex/bin strings out with the prefixs '0b' and '0x' so my general solution is this:

def pure_bin(data, no_of_bits=NO_OF_BITS):
    data = data + 2**(no_of_bits)
    return bin(data)[3:]

def pure_hex(data, no_of_bits=NO_OF_BITS):
    if (no_of_bits%4) != 0:
        no_of_bits = 4*int(no_of_bits / 4) + 4
    data = data + 2**(no_of_bits)
    return hex(data)[3:]
入怼 2024-09-17 09:22:28
hexa = '91278c4bfb3cbb95ffddc668d995bfe0'
binary = bin(int(hexa, 16))[2:]
print binary
hexa_dec = hex(int(binary, 2))[2:]
print hexa_dec
hexa = '91278c4bfb3cbb95ffddc668d995bfe0'
binary = bin(int(hexa, 16))[2:]
print binary
hexa_dec = hex(int(binary, 2))[2:]
print hexa_dec
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文