我想将1fd93d1cf1f13d0d转换为000111111110110000111101 00011101100110001 11110001 00111101 00001101

2024-09-28 23:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用了以下函数:

bin(int('1fd93d1cf1f13d0d', 16)) 

但我一直在

0b1111111011001001111010001110011110001111100010011110100001101 

作为输出。你知道吗

当我使用.zfill(64)

00b1111111011001001111010001110011110001111100010011110100001101

有人能告诉我哪里做错了吗。谢谢


Tags: 函数binintzfill
2条回答

您可以执行以下操作:

# remove 0b at the beginning
>>> bin_value = bin(int('1fd93d1cf1f13d0d',16))[2:]
>>> bin_value
'1111111011001001111010001110011110001111100010011110100001101'
>>> len(bin_value)
61
# add leading 0's to make string length multiple of 8
>>> bin_value = (8 - (len(bin_value) % 8)) * '0' + bin_value
>>> bin_value
'0001111111011001001111010001110011110001111100010011110100001101'
>>> len(bin_value)
64
>>> bytes = [bin_value[i:i+8] for i in range(0, len(bin_value), 8)]
>>> bytes
['00011111', '11011001', '00111101', '00011100', '11110001', '11110001', '00111101', '00001101']
>>> ' '.join(bytes)
'00011111 11011001 00111101 00011100 11110001 11110001 00111101 00001101'
>>> 
hex2= 0x1fd93d1cf1f13d0d
spec = '{fill}{align}{width}{type}'.format(fill='0', align='>', width=64, type='b')
bin_representation = format(hex2, spec)
print(bin_representation)

将字符串表示为希望它解决了。根据您想要的输出进一步格式化

相关问题 更多 >