Python3 bytes to hex string

23,317

Solution 1

You simply want to decode from ASCII here, your bytestring is already representing hexadecimal numbers, in ASCII characters:

>>> a = b'067b'
>>> a.decode('ascii')
'067b'

Everything you tried is interpreting the bytes as numeric data instead, either as hexadecimal numbers representing bytes or as bytes representing numeric data.

So your first attempt takes the value 06 as a hexadecimal number and turns that into the byte value 6, and 7b is turned into the byte value 123, which is the ASCII codepoint for the { character.

In your second attempt you are converting each byte to a hexadecimal representation of its numeric value. The 0 byte being interpreted as the integer number 48 (the ASCII codepoint for the '0' character), which is 30 in hexadecimal. '6' is 54, or 36 in hex, etc.

Solution 2

The bytes representation can be confusing. b'0607' is not the same as bytes([0x06,0x07]). b'0607' is actually 4 bytes [48,54,48,55] where 48 is the ascii code for 0,etc.

[x for x in b'0607'] 
[48, 54, 48, 55]

as such b'0607' is already an ascii string but represented as bytes if you want it to convert it to str you need to b'0607'.decode("ascii") (so that you construct a str from those bytes interpreting them like ascii code (as they are)).

In general, you convert bytes to hex string like this:

>>> a = binascii.hexlify(bytes([1,10,15,16,255]))
>>> a
b'010a0f10ff'
>>> type(a)
<class 'bytes'>
>>> b = a.decode("ascii")
>>> type(b)
<class 'str'>
Share:
23,317
MightyPork
Author by

MightyPork

doing some web development and embedded hacking in C

Updated on July 19, 2022

Comments

  • MightyPork
    MightyPork almost 2 years

    I have a bytes object:

    a =  b'067b'
    

    How do I get a string from it? Like this:

    "067b"
    

    I've tried:

    In [3]: str(a)
    Out[3]: "b'067b'"
    
    In [4]: import codecs
    
    In [5]: codecs.decode(a,'hex')
    Out[5]: b'\x06{'
    
    In [6]: import binascii
    
    In [7]: binascii.b2a_hex(a)
    Out[7]: b'30363762'
    
    In [8]: binascii.hexlify(a)
    Out[8]: b'30363762'
    

    Is there no way to do this?