python: string of hex values to binary -
this sample of input:
a = 41cf4a077a7454
they represent hex values, x41 xcf x4a etc...
i require convert them 1 string of binary (desired output):
01000001110011110100101000000111011110100111010001010100
x41 = 01000001 xcf = 11001111 x4a = 01001010 etc...
the code used looked this:
return bin(int(str(a), 16))[2:]
however, produces string without 0 @ front:
1000001110011110100101000000111011110100111010001010100
it appears 0 gets chopped it's interpreted integer. there way can keep zero, because not every string being converted begins 0 in binary.
i hope makes sense. thank you.
this solution work you.
>>> = '41cf4a077a7454' >>> b = [a[2*i]+a[2*i+1] in range(len(a)/2)] ['41', 'cf', '4a', '07', '7a', '74', '54'] >>> c = map(lambda x: "{0:08b}".format(int(x, 16)), b) ['01000001', '11001111', '01001010', '00000111', '01111010', '01110100', '01010100'] >>> "".join(c) '01000001000111001100111111110100010010101010000000000111'
Comments
Post a Comment