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?

sjakobi
  • 3,546
  • 1
  • 25
  • 43
Johannes
  • 113
  • 1
  • 9
  • 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. – Simeon Sep 26 '18 at 08:36

1 Answers1

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'
Mazdak
  • 105,000
  • 18
  • 159
  • 188