Python Convert Hex to Base64

3.7/5 - (3 votes)

πŸ’¬ 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!)

IndexBinaryChar
0000000A
1000001B
2000010C
3000011D
4000100E
5000101F
6000110G
7000111H
8001000I
9001001J
10001010K
11001011L
12001100M
13001101N
14001110O
15001111P
16010000Q
17010001R
18010010S
19010011T
20010100U
21010101V
22010110W
23010111X
24011000Y
25011001Z
26011010a
27011011b
28011100c
29011101d
30011110e
31011111f
32100000g
33100001h
34100010i
35100011j
36100100k
37100101l
38100110m
39100111n
40101000o
41101001p
42101010q
43101011r
44101100s
45101101t
46101110u
47101111v
48110000w
49110001x
50110010y
51110011z
521101000
531101011
541101102
551101113
561110004
571110015
581110106
591110117
601111008
611111019
62111110+
63111111/
IndexBinaryCharacter

🌍 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!