💬 Question: How to convert a hexadecimal string such as
02af01ff00 to a normal string using the Base64 format in Python?
👉 Short answer: Use the following fancy one-liner expression to convert the hex string
s to a Base64-encoded Python string:
For the long answer, keep reading! 🥸
If you’re like me, you may need a quick refresher on how Base64 encoding works and what it is exactly. Although I studied computer science a couple of years ago, I don’t have all those super basic “bits” of knowledge at the top of my head all the time.
You may already know about Base64 — in that case, I’d recommend you skip the next section and jump ahead right away. 👇
What Is Base64 Encoding?
Base64 is a very minimal encoding where a minimal set of characters —
0-9, essentially — are encoded using only six bits. Each bit position doubles the number of different encodings, so the Base64 encoding can encode 2*2*2*2*2*2 = 2^6 = 64 different characters.
Here’s the whole table — fortunately, the encoding is small and efficient enough that I can show you the whole thing! 🤯
(And, no, Emojis don’t have any place in Base64, it’s VERY old school!)
🌍 Recommended Tutorial: Python Base64 – String Encoding and Decoding [+Video]
How to Convert Base64 Encoding (Hex String) to Human-Readable String in Python?
You can convert a hex string of the format
'02af01ff00' to a Base64 encoded normal Python string by using the expression:
You can convert the resulting Base64 string back to a normal string by using the one-liner expression:
Here’s a code example—I’ll break it down for you right after the code:
import base64 s = '02af01ff00' # hex string -> base64 string b64 = base64.b64encode(bytes.fromhex(s)).decode() # base64 string -> hex string s2 = base64.b64decode(b64.encode()).hex() print(s) print(b64) print(s2)
The output shows that you successfully converte from the hex string to the Base64 string and back to the hex string:
02af01ff00 Aq8B/wA= 02af01ff00
You can see that the start and end values of the conversion remain the same.
Let’s break down the code step by step!
Step 1: The initial hex string is still in a non-standardized format
'02af01ff00'. We require it to be a
bytes object because this is the required input format of the
base64 functions shown in a moment. You use the
>>> s = '02af01ff00' >>> bytes.fromhex(s) b'\x02\xaf\x01\xff\x00'
Step 2: You use the
base64.b64encode() function to take the hex string (as
bytes object) and convert it to a
bytes object in Base64 encoding. This is almost what you want — but it’s not yet a normal Python string!
>>> base64.b64encode(bytes.fromhex(s)) b'Aq8B/wA='
Step 3: To convert the
bytes object in Base64 encoding to a normal Python string, we use the
>>> base64.b64encode(bytes.fromhex(s)).decode() 'Aq8B/wA='
Voilà, exactly what you wanted! But how to convert it back?
Step 4 and 5: You can convert the normal Base64-encoded Python string back to the hex string from the beginning by using the
string.encode() method to obtain a
bytes object, passing it into the
base64.b64decode() function to obtain a Base64
bytes representation, and converting it to a hexadecimal string by using the
>>> base64.b64decode('Aq8B/wA='.encode()) b'\x02\xaf\x01\xff\x00' >>> base64.b64decode('Aq8B/wA='.encode()).hex() '02af01ff00'
Thanks for reading through the whole tutorial, I hope you managed to solve your issue! If not, you can check out this highly interesting SO answer.
Also, make sure to check out our free Python cheat sheets for maximal learning efficiency and fun!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.