gpt4 book ai didi

python - 在 python 中将十六进制转换为 BCD

转载 作者:行者123 更新时间:2023-11-28 20:23:50 24 4
gpt4 key购买 nike

我的十六进制字符串是 0e0010000001020c93b2

它的等效 BCD 字符串(在线生成器)是 01100110000100010100001010000011011010000001011010000011100101101001000110000100011010010000

我该怎么做 python?

最佳答案

像这样,例如:

>>> ''.join(format(int(c), '04b') for c in str(int('0e0010000001020c93b2', 16)))
'0110011000010001010000101000001101101000000101\
1010000011100101101001000110000100011010010000'

解释:

>>> hex_string = '0e0010000001020c93b2'

>>> decimal_int = int(hex_string, 16)
>>> decimal_int
66114283681683969184690L

>>> decimal_string = str(decimal_int)
>>> decimal_string
'66114283681683969184690'

>>> digits = [int(c) for c in decimal_string]
>>> digits
[6, 6, 1, 1, 4, 2, 8, 3, 6, 8, 1, 6, 8, 3, 9, 6, 9, 1, 8, 4, 6, 9, 0]

>>> zero_padded_BCD_digits = [format(d, '04b') for d in digits]
>>> zero_padded_BCD_digits
['0110', '0110', '0001', '0001', '0100', '0010', '1000', '0011', '0110', '1000',
'0001', '0110', '1000', '0011', '1001', '0110', '1001', '0001', '1000', '0100',
'0110', '1001', '0000']

>>> ''.join(zero_padded_BCD_digits)
'0110011000010001010000101000001101101000000101\
1010000011100101101001000110000100011010010000'

关于python - 在 python 中将十六进制转换为 BCD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18827170/

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