No, using encode() to hexlify isn't nice.

The way you use the hex codec worked in Python 2 because you can call encode() on 8-bit strings in Python 2, ie you can encode something that is already encoded. That doesn't make sense. encode() is for encoding Unicode strings into 8-bit strings, not for encoding 8-bit strings as 8-bit strings.

In Python 3 you can't call encode() on 8-bit strings anymore, so the hex codec became pointless and was removed.

Although you theoretically could have a hex codec and use it like this:

>>> import codecs
>>> hexlify = codecs.getencoder('hex')
>>> hexlify(b'Blaah')[0]
b'426c616168'

Using binascii is easier and nicer:

>>> import binascii
>>> binascii.hexlify(b'Blaah')
b'426c616168'
Answer from Lennart Regebro on Stack Overflow
Top answer
1 of 3
52

No, using encode() to hexlify isn't nice.

The way you use the hex codec worked in Python 2 because you can call encode() on 8-bit strings in Python 2, ie you can encode something that is already encoded. That doesn't make sense. encode() is for encoding Unicode strings into 8-bit strings, not for encoding 8-bit strings as 8-bit strings.

In Python 3 you can't call encode() on 8-bit strings anymore, so the hex codec became pointless and was removed.

Although you theoretically could have a hex codec and use it like this:

>>> import codecs
>>> hexlify = codecs.getencoder('hex')
>>> hexlify(b'Blaah')[0]
b'426c616168'

Using binascii is easier and nicer:

>>> import binascii
>>> binascii.hexlify(b'Blaah')
b'426c616168'
2 of 3
2

this is the same answer for the above but i modified it so it works with python 3.

import binascii
from Crypto.Cipher import AES
from Crypto import Random

def encrypt(passwrd, message):
    msglist = []
    key = bytes(passwrd, "utf-8")
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CFB, iv)
    msg = iv + cipher.encrypt(bytes(message, "utf-8"))
    msg = binascii.hexlify(msg)
    for letter in str(msg):
        msglist.append(letter)
    msglist.remove("b")
    msglist.remove("'")
    msglist.remove("'")
    for letter in msglist:
        print(letter, end="")
    print("")

def decrypt(passwrd, message):
    msglist = []
    key = bytes(passwrd, "utf-8")
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CFB, iv)
    msg = cipher.decrypt(binascii.unhexlify(bytes(message, "utf-8")))[len(iv):]
    for letter in str(msg):
        msglist.append(letter)
    msglist.remove("b")
    msglist.remove("'")
    msglist.remove("'")
    for letter in msglist:
        print(letter, end="")
    print("")
Discussions

python - How to encode hex in Python3? - Stack Overflow
I'm tring to write equivalent for encode("hex") in python3. ... So in Python 2.7 both methods give same result. Now i tring to run this code in Python 3.5: More on stackoverflow.com
🌐 stackoverflow.com
January 2, 2020
String to HEX
I've tried .encode, binascii.hexlify, and a few other things. Ok, well, encode was the correct one, so do that. More on reddit.com
🌐 r/learnpython
7
5
September 2, 2024
How to convert character to hexadecimal and back?
I tried to code to convert string to hexdecimal and back for controle as follows: input = 'Україна' table = [] for item in input: table.append(item.encode('utf-8').hex()) output = '' for item in table: output += chr(int(item, 16)) print(output) To my surprise the output is total different ... More on discuss.python.org
🌐 discuss.python.org
6
0
March 22, 2023
python - Python3 - How to convert a string to hex - Stack Overflow
I am trying to convert a string to hex character by character, but I cant figure it out in Python3. In older python versions, what I have below works: test = "This is a test" for c in range(0, le... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python.org
discuss.python.org › python help
How to convert character to hexadecimal and back? - Python Help - Discussions on Python.org
March 22, 2023 - I tried to code to convert string ... table = [] for item in input: table.append(item.encode('utf-8').hex()) output = '' for item in table: output += chr(int(item, 16)) print(output) To my surprise the output is total different ...
🌐
Rip Tutorial
riptutorial.com › encode/decode to hex no longer available
Python Language Tutorial => encode/decode to hex no longer available
import codecs codecs.decode('1deadbeef4', 'hex') # Out: b'\x1d\xea\xdb\xee\xf4' codecs.encode(b'\x1d\xea\xdb\xee\xf4', 'hex') # Out: b'1deadbeef4'
Find elsewhere
🌐
GitHub
github.com › wilvk › blog-hugo › blob › master › content › posts › python-hex-strings.md
blog-hugo/content/posts/python-hex-strings.md at master · wilvk/blog-hugo
October 10, 2019 - If we start again with our original string AAA-BBB and we want to convert that to it's hexadecimal representation, it turns out there are a few ways to do this. The simplest way is to call .encode() on the string, and then .hex() on the byte array.
Author: wilvk
🌐
Hex to String
hextostring.com › post › pythonstrtohex
Python String to Hex: encode().hex() and Pitfalls | Hex to String Guides
July 22, 2026 - Python string → hex is: encode text to bytes, then render those bytes as hex digits. The usual path is str.encode(...).hex(). Opposite direction: hex to string methods. Tools: String to Hex · Hex to String. text = "Hello" print(text.enco...
🌐
Delft Stack
delftstack.com › home › howto › python › str to hex python
How to Convert String to Hex in Python | Delft Stack
February 2, 2024 - In conclusion, this article explored diverse methods for converting a string to its hexadecimal representation in Python. The encode() method proved fundamental, allowing the transformation of a string into bytes, which could then be processed using functions like hex().
🌐
GitHub
gist.github.com › Nurdok › 4096182
Python Conversion · GitHub
April 20, 2013 - With regards to encode / decode hex: In Python 3.2+, you can do codecs.encode(b"45", "hex_codec").
🌐
AskPython
askpython.com › python › examples › convert-bytes-to-hex-strings
4 Handy Ways to Convert Bytes to Hex Strings in Python 3 - AskPython
May 16, 2023 - You can use Python’s built-in modules like codecs, binascii, and struct or directly leverage the bytes.hex() function. Each method is efficient and easy to implement, providing a flexible approach to byte-to-hex conversions.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-hex-to-string-in-python
Convert Hex to String in Python - GeeksforGeeks
July 23, 2025 - The codecs module can directly decode a hex string into bytes, which can then be converted to a string.
🌐
Linux Hint
linuxhint.com › string-to-hexadecimal-in-python
String to Hexadecimal in Python – Linux Hint
We can also convert the normal string to a hexadecimal string that doesn’t have any hexadecimal character. For that, we have to convert the string to byte using the method encode (), and then we can convert the string to hexadecimal as shown below:
🌐
Medium
medium.com › @nawazmohtashim › convert-hex-to-string-in-python-a-comprehensive-guide-36d4f128cd9e
Convert Hex to String in Python: A Comprehensive Guide | by Mohd Mohtashim Nawaz | Medium
December 13, 2023 - This method involves iterating through pairs of hex digits, converting them to integers, and then using chr() to obtain the corresponding character. The bytes.fromhex() method is available in Python 3 and is specifically designed for converting ...
🌐
GitHub
github.com › vadimkantorov › wemosetup › issues › 7
encode hex no longer working in python3 · Issue #7 · vadimkantorov/wemosetup
February 2, 2021 - Looks like the encode hex command in the encrypt_wifi_password is failing in python3 when trying to add a device. I tried importing codecs and changing it to codecs.encode(salt, 'hex') and codecs.encode(iv,'hex') but it still seems to fail with it complaining about it being a string and not byte coded `` Discovered 3 networks with SSID "wifissid", using the first available..."
Author: vadimkantorov