Python has bytes-to-bytes standard codecs that perform convenient transformations like quoted-printable (fits into 7bits ascii), base64 (fits into alphanumerics), hex escaping, gzip and bz2 compression. In Python 2, you could do: b'foo'.encode('hex')
In Python 3, str.encode / bytes.decode are strictly for bytes<->str conversions. Instead, you can do this, which works across Python 2 and
Python 3 (s/encode/decode/g for the inverse): import codecs
codecs.getencoder('hex')(b'foo')[0]
Starting with Python 3.4, there is a less awkward option: codecs.encode(b'foo', 'hex')
These misc codecs are also accessible inside their own modules (base64, zlib, bz2, uu, quopri, binascii); the API is less consistent, but for compression codecs it offers more control. - HowTo
- Python How-To's
- Convert Byte to Hex in Python
Created: December-03, 2020 | Updated: March-08, 2021
- Initialize a Byte Literal in Python
- Use the
hex() Method to Convert a Byte to Hex in Python - Use the
binascii Module to Convert a Byte to Hex in Python
This tutorial will introduce how to convert bytes into hexadecimal in Python. The byte data type in Python is a sequence of bytes that can be stored on the disk as a variable, which can then be encoded and decoded. They are declared like a string but
prefixed by the character b . Bytes accept special Unicode characters prefixed with \x . Initialize a Byte Literal in PythonWe will give an example of a byte literal, declare a string with special characters, and use the function encode('utf-8') to convert it to a byte literal. byte_var = 'γιαούρτι - yogurt'.encode('utf-8')
print(byte_var)
Output:
b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
The output of encode() will result in a byte literal prefixed with the character b and the special characters converted
into Unicode symbols. Now the declaration of a byte is covered, let’s proceed with converting a byte into hex. Use the hex() Method to Convert a Byte to Hex in PythonThe hex() method introduced from Python 3.5 converts it into a hexadecimal string. In this case, the argument will be of a byte data type to be converted
into hex. byte_var = 'γιαούρτι - yogurt'.encode('utf-8')
print('Byte variable: ', byte_var)
print('Hexadecimal: ', byte_var.hex())
Output: Byte variable: b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
Hexadecimal: ceb3ceb9ceb1cebfcf8dcf81cf84ceb9202d20796f67757274
Use the binascii Module to Convert a Byte to Hex in PythonThe binascii Python module contains efficient utility functions for binary and ASCII operations. Within this module, there is a function hexlify() that returns a hexadecimal value of
the given argument, which is a binary value. In this example, the argument will be the byte variable to be converted into hex. import binascii
byte_var = 'γιαούρτι - yogurt'.encode('utf-8')
print('Byte variable: ', byte_var)
print('Hexadecimal: ', binascii.hexlify(byte_var))
Output: Byte variable: b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
Hexadecimal: b'ceb3ceb9ceb1cebfcf8dcf81cf84ceb9202d20796f67757274'
Take note that the return value of hexlify() returns a byte literal, unlike hex() , which returns a converted string. If you want to convert the result into a string, use the function decode('utf-8') . import binascii
byte_var = 'γιαούρτι - yogurt'.encode('utf-8')
print('Byte variable: ', byte_var)
print('Hexadecimal: ', '' + binascii.hexlify(byte_var).decode('utf-8'))
Output: Byte variable: b'\xce\xb3\xce\xb9\xce\xb1\xce\xbf\xcf\x8d\xcf\x81\xcf\x84\xce\xb9 - yogurt'
Hexadecimal: ceb3ceb9ceb1cebfcf8dcf81cf84ceb9202d20796f67757274
Now the hexadecimal result is converted into a string from a byte literal. In summary, we’ve
covered 2 methods of converting a byte to hex in Python. The simplest way is to use the built-in function hex() to a byte literal. Alternatively, the hexlify() function from the binascii module can also be used to produce the same output. Related Article - Python BytesConvert Bytes to Int in Python 2.7 and 3.xConvert Int to Bytes in Python 2 and Python 3Convert Int to Binary in PythonConvert Bytes to String in Python 2 and Python 3Related Article - Python
HexConvert Bytes to Int in Python 2.7 and 3.xConvert Int to Bytes in Python 2 and Python 3Convert Int to Binary in PythonConvert Bytes to String in Python 2 and Python 3
How do you display bytes as hex in python?
Use bytearray.. byte_array = bytearray(b"\xab"). hexadecimal_string = byte_array. hex(). print(hexadecimal_string).
Is byte A hex?
A byte (or octet) is 8 bits so is always represented by 2 Hex characters in the range 00 to FF.
How do you convert int to hex in python?
hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.
Can we convert string to hex in python?
We can convert the string to hexadecimal using the following methods: Using the hex(n) method. Using the encode () method. Using the literal_eval () method.
|