Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to use base64.b64decode() in Python

Ayesha Naeem

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.

  • s contains a byte-like object that is to be decoded.
  • The altchars parameter specifies the alternative characters encoded instead of + or / characters. altchars is a byte-like object and must have a minimum length of 2.
  • If the validate parameter is False, the function will discard all non-base64 or non-alternative characters in s; otherwise, it raises the binascii.Error exception.

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 the utf-8 encoding.
  • 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 encode
string = 'pppfoo???'
#convert string to bytes
string_encode = string.encode('utf-8')
#ecode in base 64
encoded = base64.b64encode(string_encode)
#display encode data
print(encoded)
#decode from base 64
decoded = base64.b64decode(encoded)
#convert from bytes to string and display
print(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 the utf-8 encoding.
  • The program then encodes the bytes in base64 and specifies in the altchars argument 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. The altchars parameter 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 altchars parameter while decoding, a binascii.Error exception will be thrown, as : does not belong to the normal base64 alphabets.

import base64

#string to encode
string = 'pppfoo???'
#convert string to bytes
string_encode = string.encode('utf-8')
#ecode in base 64
encoded = base64.b64encode(string_encode, altchars=b'-:')
#display encode data
print(encoded)
#decode from base 64
decoded = base64.b64decode(encoded,  altchars=b'-:')
#convert from bytes to string and display
print(decoded.decode('utf-8'))

If the padding of the encoded data is incorrect, the program throws a binascii.Error exception.

RELATED TAGS

python

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring