What's the correct way to convert bytes to a hex string in Python 3?

Matt Joiner :

What's the correct way to convert bytes to a hex string in Python 3?

I see claims of a bytes.hex method, bytes.decode codecs, and have tried other possible functions of least astonishment without avail. I just want my bytes as hex!

Felix Weis :

Since Python 3.5 this is finally no longer awkward:

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'

and reverse:

>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

works also with the mutable bytearray type.

Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fastest way to convert a dict's keys & values from `bytes` to `str` in python3

Best way to convert string to bytes in Python 3?

Best way to convert string to bytes in Python 3?

Efficient way to convert an int to Hex string and then pad it with 0's in Golang?

Python3 bytes to hex string

What's the fastest way to convert hex to integer in C++?

How to convert a hex string to bytes array in Crystal?

What is the correct code to convert to hex in below format?

What is correct way to convert QGyroscopeReading to QVector3D?

How to convert hex string starting with 0 to bytes with certain lenght in python

How to convert hex string to bytes

How to convert hex string to hex bytes format in PySpark

When making binary masks for working and isolating bits and bytes, what's the fastest way to convert from Binary to Hex?

What is the correct way to slice a unicode string in python?

convert string to hex in python

How to convert from binary to bytes to hex in Python 3?

Converting a hex string into the correct form for the bytes function

Python: Convert hex to string

Convert ASCII data to hex/binary/bytes in Python

Python String to Hex Bytes

Efficient way to split a bytes array then convert it to string in Python

What's the correct way to append a variable to a string?

What is the correct way to convert bytes to a hex string in Python 3?

Convert hex Ascii representation in a bytes object to Ascii string using Python3

Convert hex string to characters in python3

What's the correct way to convert from StringBuilder to String?

How to convert bytes3 to HEX string in Solidity

What's a less bad way to write this hex formatting function in Python?

Convert bytes to a string in python 3