π¬ 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: base64.b64encode(bytes.fromhex(s)).decode()
.
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 — A-Z
, a-z
, and 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!)
Index | Binary | Char |
---|---|---|
0 | 000000 | A |
1 | 000001 | B |
2 | 000010 | C |
3 | 000011 | D |
4 | 000100 | E |
5 | 000101 | F |
6 | 000110 | G |
7 | 000111 | H |
8 | 001000 | I |
9 | 001001 | J |
10 | 001010 | K |
11 | 001011 | L |
12 | 001100 | M |
13 | 001101 | N |
14 | 001110 | O |
15 | 001111 | P |
16 | 010000 | Q |
17 | 010001 | R |
18 | 010010 | S |
19 | 010011 | T |
20 | 010100 | U |
21 | 010101 | V |
22 | 010110 | W |
23 | 010111 | X |
24 | 011000 | Y |
25 | 011001 | Z |
26 | 011010 | a |
27 | 011011 | b |
28 | 011100 | c |
29 | 011101 | d |
30 | 011110 | e |
31 | 011111 | f |
32 | 100000 | g |
33 | 100001 | h |
34 | 100010 | i |
35 | 100011 | j |
36 | 100100 | k |
37 | 100101 | l |
38 | 100110 | m |
39 | 100111 | n |
40 | 101000 | o |
41 | 101001 | p |
42 | 101010 | q |
43 | 101011 | r |
44 | 101100 | s |
45 | 101101 | t |
46 | 101110 | u |
47 | 101111 | v |
48 | 110000 | w |
49 | 110001 | x |
50 | 110010 | y |
51 | 110011 | z |
52 | 110100 | 0 |
53 | 110101 | 1 |
54 | 110110 | 2 |
55 | 110111 | 3 |
56 | 111000 | 4 |
57 | 111001 | 5 |
58 | 111010 | 6 |
59 | 111011 | 7 |
60 | 111100 | 8 |
61 | 111101 | 9 |
62 | 111110 | + |
63 | 111111 | / |
Index | Binary | Character |
π 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:
base64.b64encode(bytes.fromhex(s)).decode()
You can convert the resulting Base64 string back to a normal string by using the one-liner expression:
base64.b64decode(b64.encode()).hex()
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 bytes.fromhex()
function.
>>> 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 bytes.decode()
method.
>>> 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 bytes.hex()
method.
>>> base64.b64decode('Aq8B/wA='.encode()) b'\x02\xaf\x01\xff\x00' >>> base64.b64decode('Aq8B/wA='.encode()).hex() '02af01ff00'
Thanks β€οΈ
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!