How to use base64.b64decode() in Python
The base64.b4() function in Python decodes strings or byte-like objects encoded in base64 and returns the decoded bytes.
Prototype and parameters
The base64.b64decode() function takes s as the only mandatory parameter.
scontains a byte-like object that is to be decoded.- The
altcharsparameter specifies the alternative characters encoded instead of+or/characters.altcharsis a byte-like object and must have a minimum length of 2. - If the
validateparameter isFalse, the function will discard all non-base64 or non-alternative characters ins; otherwise, it raises thebinascii.Errorexception.
Note: The
base64.b64encode()function encodes 3 bytes at a time. If the last set of bytes contains less than 3 bytes, its encoding is padded with the=character. One=character means the last set contains two bytes, and two=characters mean the last set contains a single byte.
Examples
Example 1
The following code demonstrates how to decode a byte-like object in base64 without alternative characters.
- The program obtains the binary form of the string
pppfoo???through theutf-8encoding. - The program then encodes the bytes in base64 and displays the encoded data.
- To check the correctness of the encoding, the program decodes the encoded data using the
base64.b64decode()function. - The decoded form is converted from binary to string and displayed. The initial and final strings are identical, as illustrated.
import base64#string to encodestring = 'pppfoo???'#convert string to bytesstring_encode = string.encode('utf-8')#ecode in base 64encoded = base64.b64encode(string_encode)#display encode dataprint(encoded)#decode from base 64decoded = base64.b64decode(encoded)#convert from bytes to string and displayprint(decoded.decode('utf-8'))
Example 2
The following program demonstrates how to decode a byte-like object in base64 with alternative characters.
- The program obtains the binary form of the string
pppfoo???through theutf-8encoding. - The program then encodes the bytes in base64 and specifies in the
altcharsargument that/or+is to be encoded with:. - To check the correctness of the encoding, the program decodes the encoded data using the
base64.b64decode()function. Thealtcharsparameter is input to specify that/or+is encoded as:. - The decoded form is converted from binary to strings and displayed. The initial and final strings are identical, as illustrated.
If the program does not specify the
altcharsparameter while decoding, abinascii.Errorexception will be thrown, as:does not belong to the normal base64 alphabets.
import base64#string to encodestring = 'pppfoo???'#convert string to bytesstring_encode = string.encode('utf-8')#ecode in base 64encoded = base64.b64encode(string_encode, altchars=b'-:')#display encode dataprint(encoded)#decode from base 64decoded = base64.b64decode(encoded, altchars=b'-:')#convert from bytes to string and displayprint(decoded.decode('utf-8'))
If the padding of the encoded data is incorrect, the program throws a
binascii.Errorexception.
Free Resources