If you too have been personally victimized by Python3’s 'str' object has no attribute 'decode' exception
and other string/bytes-related exceptions, I feel your agony. Trauma from such errors have stopped me from using Python3 for code handling buffers, like POCs for vulnerabilities or CTF exploits. Here’s a reference guide on how to convert between Python3’s hexstr/str/bytes/bytearray.
Ultimate Python3 Type Conversion Chart
This has helped me when it was in my personal notes. Hopefully, now it can help you, too.
have mystr of type str | have myhexstr of type hexstr | have mybytes of type bytes | have mybytearray of type bytearray | |
---|---|---|---|---|
want mystr of type str | no change needed | mystr=binascii.unhexlify(myhexstr).decode(<*encoding_see_bottom_note>) |
mystr = mybytes.decode(<*encoding_see_bottom_note>) |
mystr = mybytearray.decode(<*encoding_see_bottom_note>) |
want hexstr of type hexstr | myhexstr = binascii.hexlify(mystr).decode(“utf-8”) |
no change needed | myhexstr = binascii.hexlify(bytearray(mybytes)).decode(“utf-8”) |
myhexstr = binascii.hexlify(mybytearray).decode(“utf-8”) |
want bytes of type bytes | mybytes = mystr.encode() |
mybytes = binascii.unhexlify(myhexstr) |
no change needed | mybytes = bytes(mybytearray) |
want bytearray of type bytearray | mybytearray = bytearray(mystr.encode()) |
mybytearray = bytearray(binascii.unhexlify(myhexstr)) |
mybytearray = bytearray(mybytes) |
no change needed |
*encoding is usually “ascii”/”utf-8” (they’re the same thing), but for non-English character sets, the encoding might be “utf-16”, “utf-32.”
Other Python3 Buffer Need-To-Know
Convert bytes
to/from list of ints
Convenient for binary/mathematical operations such as XOR between two buffers
bytes to list of ints
myintlist = list(mybytes)
list of ints to bytes
mybytes = bytes(myintlist)
Read bytes and str from files
Read data from a file as str
Open the file using “r” Example:
mystr = open("C:\whtaguy\orderyogurt.py", "r").read()
Read data from a file as bytes
Open the file using “rb” Example:
mybytes = open("C:\whtaguy\orderyogurt.py", "rb").read()
Python3 Buffer Type Review
str
- Example:
mystr = “don’t forget your daily calcium”
- An immutable unicode string
- Created statically using quotes.
hexstring
- Example: “calc” is
“63616c63”
- A str consisting of hexadecimal numbers (0-9, a-f).
- Primarily used to convert binary data to a printable format.
- Created like str, but contains only hexadecimal numbers
bytes
- Example:
mybytes = b“bring all the boys to the yard”
- An immutable array of one-byte elements
- Created statically by putting the letter “b” before quotes
bytearray
- Example:
mybytearray = bytearray(b”I’ll grow up to be in a bytearray”)
- A mutable list of one-byte elements
- Created through the
bytearray
constructor, which is initialized with a more primitive type (such as bytes, str)