![]() |
|
[Tut] Python Convert Hex to Base64 - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Convert Hex to Base64 (/thread-100318.html) |
[Tut] Python Convert Hex to Base64 - xSicKxBot - 11-30-2022 Python Convert Hex to Base64 5/5 – (1 vote) Question: How to convert a hexadecimal string such as Code: 02af01ff00![]() Short answer: Use the following fancy one-liner expression to convert the hex string Code: sCode: 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 — Code: A-ZCode: a-zCode: 0-9Here’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 Code: '02af01ff00'Code: base64.b64encode(bytes.fromhex(s)).decode()You can convert the resulting Base64 string back to a normal string by using the one-liner expression: Code: base64.b64decode(b64.encode()).hex()Here’s a code example—I’ll break it down for you right after the code: 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: Code: 02af01ff00
Aq8B/wA=
02af01ff00You 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 Code: '02af01ff00'Code: bytesCode: base64Code: bytes.fromhex()Code: >>> s = '02af01ff00'
>>> bytes.fromhex(s)
b'\x02\xaf\x01\xff\x00'Step 2: You use the Code: base64.b64encode()Code: bytesCode: bytesCode: >>> base64.b64encode(bytes.fromhex(s))
b'Aq8B/wA='Step 3: To convert the Code: bytesCode: bytes.decode()Code: >>> 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 Code: string.encode()Code: bytesCode: base64.b64decode()Code: bytesCode: bytes.hex()Code: >>> 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! |