Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex to Base64 conversion in Python

Tags:

I want to convert a simple HEX string such as 10000000000002ae to Base64.

The hex string is to be converted to bytes, and the bytes are then encoded to base64 notation, so the expected output for that string: EAAAAAAAAq4=

I found a tool online: http://tomeko.net/online_tools/hex_to_base64.php?lang=en

But I have a bunch of HEX values that I need to convert in a script.

like image 853
React Native Noob Avatar asked Nov 14 '15 01:11

React Native Noob


People also ask

What is Base64 b64decode in Python?

b64decode() in Python. With the help of base64. b64decode() method, we can decode the binary string into normal form.

How do I decode a Base64 string in Python?

To decode an image using Python, we simply use the base64. b64decode(s) function. Python mentions the following regarding this function: Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes.


2 Answers

Edit 26 Aug 2020: As suggested by Ali in the comments, using codecs.encode(b, "base64") would result in extra line breaks for MIME syntax. Only use this method if you do want those line breaks formatting.

For a plain Base64 encoding/decoding, use base64.b64encode and base64.b64decode. See the answer from Ali for details.


In Python 3, arbitrary encodings including Hex and Base64 has been moved to codecs module. To get a Base64 str from a hex str:

import codecs  hex = "10000000000002ae" b64 = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode() 
like image 60
Eana Hufwe Avatar answered Sep 22 '22 15:09

Eana Hufwe


from base64 import b64encode, b64decode  # hex -> base64 s = 'cafebabe' b64 = b64encode(bytes.fromhex(s)).decode() print('cafebabe in base64:', b64)  # base64 -> hex s2 = b64decode(b64.encode()).hex() print('yv66vg== in hex is:', s2) assert s == s2 

This prints:

 cafebabe in base64: yv66vg== yv66vg== in hex is: cafebabe 

The relevant functions in the documentation, hex to base64:

  • b64encode
  • bytes.fromhex
  • bytes.decode

Base64 to hex:

  • b64decode
  • str.encode
  • bytes.hex

I don't understand why many of the other answers are making it so complicated. For example the most upvoted answer as of Aug 26, 2020:

  • There is no need for the codecs module here.
  • The codecs module uses base64.encodebytes(s) under the hood (see reference here), so it converts to multiline MIME base64, so you get a new line after every 76 bytes of output. Unless you are sending it in e-mail, it is most likely not what you want.

As for specifying 'utf-8' when encoding a string, or decoding bytes: It adds unnecessary noise. Python 3 uses utf-8 encoding for strings by default. It is not a coincidence that the writers of the standard library made the default encoding of the encode/decode methods also utf-8, so that you don't have to needlessly specify the utf-8 encoding over and over again.

like image 40
Ali Avatar answered Sep 20 '22 15:09

Ali