0

I'm trying to convert hexdata into binary and then back to hex. I'm getting hexdata but as byte object

    hexdata='91278c4bfb3cbb95ffddc668d995bfe0'
    b=binascii.a2b_hex(hexdata)
    print (b)
    b"\x91'\x8cK\xfb<\xbb\x95\xff\xdd\xc6h\xd9\x95\xbf\xe0"
    binascii.b2a_hex(b)
    b'91278c4bfb3cbb95ffddc668d995bfe0'

I'm expecting this to come as simple string (as my input i.e., hexstring) that I can use in my code.

2 Answers 2

1

Just decode the bytestring as ASCII:

binascii.b2a_hex(b).decode('ASCII')

as hex digits are limited to the characters 0-9 and a-f.

Sign up to request clarification or add additional context in comments.

Comments

0

You may try to use the following code:

hexa = '91278c4bfb3cbb95ffddc668d995bfe0'
binary = bin(int(hexa, 16))[2:]
print binary
hexa_dec = hex(int(binary, 2))[2:]
print hexa_dec

1 Comment

Dead Sir, you have formed a habit of answering questions with code only and no explaining text. Please write a few lines about the code you give as answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.