十六进制到二进制字符串和所需的整数和

时间:2017-08-07 23:07:00

标签: python binary hex

我正在写一个简单的十六进制到二进制函数..

如果十六进制代码是100%数字,那么它转换正常.. (在此示例中列出前2个转换,并在3日失败)

然而,当涉及到一封信时,我得到了一个

ValueError: invalid literal for int() with base 10: 

由于字符串。

我会绕着圈子尝试str和int来解决所有事情。

我觉得我唯一能做的就是创建一个十六进制数字表,然后将'B'转换为11例如..

输入是dec_to_hex列表,预期输出应为:

“01101000 01100101 01101100 01101100 01101111 00100000 01001101 01111001 00100000 01001110 01100001 01101101 01100101 00100000 01001001 01110011 00100000 01001010 01101111 01101000 01101110”

dec_to_hex =['68', '65', '6C', '6C', '6F', '20', '4D', '79', '20', '4E', '61', '6D', '65', '20', '49', '73', '20', '4A', '6F', '68', '6E']

def hex2binary(hex_num):
    """ converts from hexadecimal to binary """
    h0 = "0000"
    h1 = "0001"
    h2 = "0010"
    h3 = "0011"
    h4 = "0100"
    h5 = "0101"
    h6 = "0110"
    h7 = "0111"
    h8 = "1000"
    h9 = "1001"
    h10 = "1010"
    h11 = "1011"
    h12 = "1100"
    h13 = "1101"
    h14 = "1110"
    h15 = "1111"

    hex1 = int(hex_num[0])
    hex2 = int(hex_num[1])

    if hex1 == 0:  # if hex 1 is the same as h0
        hex1 = h0  # then hex1 is equal to 0
    elif hex1 == 1:
        hex1 = h1
    elif hex1 == 2:
        hex1 = h2
    elif hex1 == 3:
        hex1 = h3
    elif hex1 == 4:
        hex1 = h4
    elif hex1 == 5:
        hex1 = h5
    elif hex1 == 6:
        hex1 = h6
    elif hex1 == 7:
        hex1 = h7
    elif hex1 == 8:
        hex1 = h8
    elif hex1 == 9:
        hex1 = h9
    elif hex1 == 'A':
        hex1 = h10
    elif hex1 == 'B':
        hex1 = h11
    elif hex1 == "C":
        hex1 = h12
    elif hex1 == "D":
        hex1 = h13
    elif hex1 == "E":
        hex1 = h14
    elif hex1 == "F":
        hex1 = h15

    if hex2 == 0:  # if hex 1 is the same as h0
        hex2 = h0  # then hex2 is equal to 0
    elif hex2 == 1:
        hex2 = h1
    elif hex2 == 2:
        hex2 = h2
    elif hex2 == 3:
        hex2 = h3
    elif hex2 == 4:
        hex2 = h4
    elif hex2 == 5:
        hex2 = h5
    elif hex2 == 6:
        hex2 = h6
    elif hex2 == 7:
        hex2 = h7
    elif hex2 == 8:
        hex2 = h8
    elif hex2 == 9:
        hex2 = h9
    elif hex2 == 'A':
        hex2 = h10
    elif hex2 == 'B':
        hex2 = h11
    elif hex2 == "C":
        hex2 = h12
    elif hex2 == "D":
        hex2 = h13
    elif hex2 == "E":
        hex2 = h14
    elif hex2 == "F":
        hex2 = h15

    return str(hex1) + str(hex2)

print(hex2binary(str(dec_to_hex[0])))

hex_to_bin = [hex2binary(item) for item in dec_to_hex[0:2]]
print(hex_to_bin)

hex_to_bin = [hex2binary(item) for item in dec_to_hex[0:2]]
print(hex_to_bin)

3 个答案:

答案 0 :(得分:1)

错误来自以下几行:

hex1 = int(hex_num[0])
hex2 = int(hex_num[1])

当您使用字符作为输入调用int()时,Python会尝试将其读作十进制数字(基数为10)。所以ValueError来自传递给int的字母(A-F)。

这可以通过将强制转换为int并用数字文字替换每个数字来解决。

if hex1 == '0':
    hex1 = h0
elif hex1 == '1':
    hex1 = h1
elif hex1 == '2':
    hex1 = h2
...

答案 1 :(得分:1)

作为替代方案,您可以在此处使用bin将十六进制字符串转换为二进制,并将str.zfill(..)转换为:

>>> my_hex = '68'
>>> ''.join(bin(int(s, 16))[2:].zfill(4) for s in my_hex)
'01100110'

因此,对于您的hex_to_dec列表,您可以这样做:

def convert_hex_to_dec(h):
    return ''.join(bin(int(s, 16))[2:].zfill(4) for s in h)

dec_to_hex =['68', '65', '6C', '6C', '6F', '20', '4D', '79', '20', '4E', '61', '6D', '65', '20', '49', '73', '20', '4A', '6F', '68', '6E']

new_list = [convert_hex_to_dec(h) for h in dec_to_hex]
# where `new_list` will hold:
# ['01101000', '01100101', '01101100', '01101100', '01101111', '00100000', '01001101', '01111001', '00100000', '01001110', '01100001', '01101101', '01100101', '00100000', '01001001', '01110011', '00100000', '01001010', '01101111', '01101000', '01101110']

答案 2 :(得分:0)

@Artyer

你的回答最好!!

请您发布您的建议,以便我可以信任您!

def hex2binary(hex_num):
    """ converts from hexadecimal to binary """

    h = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'}

    hex1 = h[hex_num[0]]
    hex2 = h[hex_num[1]]


    return str(hex1) + str(hex2)

print(hex2binary(str(dec_to_hex[0])))

hex_to_bin = [hex2binary(item) for item in dec_to_hex]
print(hex_to_bin)