Reputation: 2211
I'm writing a simple hex to binary function..
if the hex code is 100% numbers then it converts fine.. (in this example list the first 2 convert and it fails on the 3rd)
however when there is a letter involved i get a
ValueError: invalid literal for int() with base 10:
due to the string.
I am going round in circles trying str and int on everything..
I feel like the only thing i can do is to create a hex to digit table and then convert from say 'B' to 11 for example..
The input is the dec_to_hex list and the expected output should be:
"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)
Upvotes: 0
Views: 345
Reputation: 48037
As an alternative, you may use bin
here to convert the hex- string to binary with str.zfill(..)
as:
>>> my_hex = '68'
>>> ''.join(bin(int(s, 16))[2:].zfill(4) for s in my_hex)
'01100110'
Hence for your hex_to_dec
list, you may do it like:
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']
Upvotes: 1
Reputation: 2211
@Artyer
Your answer is best!!
Please can you post your suggestion so i can credit you!
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)
Upvotes: 0
Reputation: 1003
The error comes from these lines:
hex1 = int(hex_num[0])
hex2 = int(hex_num[1])
when you call int()
with a character as an input then Python tries to read it as a decimal digit (base 10). So the ValueError is coming from a letter (A-F) being passed to int.
This can be solved simply by committing the cast to int and replacing each digit with digit literals.
if hex1 == '0':
hex1 = h0
elif hex1 == '1':
hex1 = h1
elif hex1 == '2':
hex1 = h2
...
Upvotes: 1