Encoding and Decoding Base64 Strings in Python
Last Updated :
07 Aug, 2024
The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss about Base64 encoding and decoding and its uses to encode and decode binary and text data.
Base64 encoding:
It is a type of conversion of bytes to ASCII characters. the list of available Base64 characters are mentioned below:
- 26 uppercase letters
- 26 lowercase letters
- 10 numbers
- + and / for new lines
Each Base64 character represents 6 bits of data. it is also important to note that it is not meant for encryption for obvious reasons. To convert a string into a Base64 character the following steps should be followed:
- Get the ASCII value of each character in the string.
- Compute the 8-bit binary equivalent of the ASCII values
- Convert the 8-bit characters chunk into chunks of 6 bits by re-grouping the digits
- Convert the 6-bit binary groups to their respective decimal values.
- Use the Base64 encoding table to align the respective Base64 values for each decimal value.
The below image provides us with a Base64 encoding table.
Image Source: WikipediaUsing python to encode strings:
In Python the base64 module is used to encode and decode data. First, the strings are converted into byte-like objects and then encoded using the base64 module. The below example shows the implementation of encoding strings isn't base64 characters.
Example:
Python3 1==
import base64
sample_string = "GeeksForGeeks is the best"
sample_string_bytes = sample_string.encode("ascii")
base64_bytes = base64.b64encode(sample_string_bytes)
base64_string = base64_bytes.decode("ascii")
print(f"Encoded string: {base64_string}")
Output:
Encoded string: R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA==
Using Python to decode strings:
Decoding Base64 string is exactly opposite to that of encoding. First we convert the Base64 strings into unencoded data bytes followed by conversion into bytes-like object into a string. The below example depicts the decoding of the above example encode string output.
Example:
Python3 1==
import base64
base64_string =" R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA =="
base64_bytes = base64_string.encode("ascii")
sample_string_bytes = base64.b64decode(base64_bytes)
sample_string = sample_string_bytes.decode("ascii")
print(f"Decoded string: {sample_string}")
Output:
Decoded string: GeeksForGeeks is the best
Similar Reads
base64.decodestring(s) in Python With the help of base64.decodestring(s) method, we can decode the binary string with the help of base64 data into normal form. Syntax : base64.decodestring(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using base64.decodestring(s) method, we are able t
1 min read
How Base64 Encoding and Decoding is Done in Node.js ? Base64 encoding and decoding are when you need to encode binary data into a text format that can be easily transmitted over text-based protocols like HTTP. Node.js provides built-in methods for Base64 encoding and decoding, which can be easily performed using the Buffer class.Table of ContentApproac
3 min read
base64.encodestring(s) in Python With the help of base64.encodestring(s) method, we can encode the string using base64 encoded data into the binary form. Syntax : base64.encodestring(string) Return : Return the encoded string. Example #1 : In this example we can see that by using base64.encodestring(s) method, we are able to get th
1 min read
Encoding and Decoding Custom Objects in Python-JSON JSON as we know stands for JavaScript Object Notation. It is a lightweight data-interchange format and has become the most popular medium of exchanging data over the web. The reason behind its popularity is that it is both human-readable and easy for machines to parse and generate. Also, it's the mo
5 min read
How to Encrypt and Decrypt Strings in Python? In this article, we will learn about Encryption, Decryption and implement them with Python. Encryption:Encryption is the process of encoding the data. i.e converting plain text into ciphertext. This conversion is done with a key called an encryption key.Decryption:Decryption is the process of decodi
5 min read
base64.decodebytes(s) in Python With the help of base64.decodebytes(s) method, we can decode the binary string with the help of base64 data into normal form. Syntax : base64.decodebytes(b_string) Return : Return the decoded string. Example #1 : In this example we can see that by using base64.decodebytes(s) method, we are able to g
1 min read