1

I have two variables of the class bytes in python3.

print(string1) --> b'2900BCE03604093C000080'
print(bytes.fromhex(string1.decode('utf8'))) --> b')\x00\xbc\xe06\x04\t<\x00\x00\x80'

print(type(string1)) --> <class 'bytes'>
print(type(bytes.fromhex(string1.decode('utf8')))) --> <class 'bytes'>

The strange values in the second output are there because of ascii interpretation of some hex-values.

My Question is how to convert the string1 more easily to the output of the second line. Is there a better way?

1
  • 2
    Maybe this is a duplication but I was confused by the other answer. A 'normal' string has been used as input in the other discussion and here the author uses a b'..' style 'string'. Please note, I am confused and may not use the proper wording. Corrections regarding this welcom. Commented Sep 26, 2018 at 8:36

1 Answer 1

2

You can use binascii.a2b_hex() function to get the hexadecimal representation of the binary data:

In [5]: binascii.a2b_hex(s)
Out[5]: b')\x00\xbc\xe06\x04\t<\x00\x00\x80'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.