I want to convert a simple HEX string such as 10000000000002ae to Base 64.
The hex string is to be converted to bytes, and the bytes are then encoded to base64 notation,
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()