Python code to convert to base64

Base64 encoding is a process of converting binary data to an ASCII string format by converting that binary data into a 6-bit character representation. The Base64 method of encoding is used when binary data, such as images or video, is transmitted over systems that are designed to transmit data in a plain-text (ASCII) format.

Follow this link for further details about understanding and working of base64 encoding.

For those who want to implement base64 encoding from scratch for the sake of understanding, here's the code that encodes the string to base64.

encoder.py

#!/usr/bin/env python3.10

class Base64Encoder:

    #base64Encoding maps integer to the encoded text since its a list here the index act as the key
    base64Encoding:list = None

    #data must be type of str or bytes
    def encode(data)->str:
        #data = data.encode("UTF-8")

        if not isinstance(data, str) and not isinstance(data, bytes):
            raise AttributeError(f"Expected {type('')} or {type(b'')} but found {type(data)}")

        if isinstance(data, str):
            data = data.encode("ascii")

        if Base64Encoder.base64Encoding == None:
            #construction base64Encoding
            Base64Encoder.base64Encoding = list()
            #mapping A-Z
            for key in range(0, 26):
                Base64Encoder.base64Encoding.append(chr(key + 65))
            #mapping a-z
            for key in range(0, 26):
                Base64Encoder.base64Encoding.append(chr(key + 97))
            #mapping 0-9
            for key in range(0, 10):
                Base64Encoder.base64Encoding.append(chr(key + 48))
            #mapping +
            Base64Encoder.base64Encoding.append('+')
            #mapping /
            Base64Encoder.base64Encoding.append('/')


        if len(data) == 0:
            return ""
        length=len(data)

        bytes_to_append = -(length%3)+(3 if length%3 != 0 else 0)
        #print(f"{bytes_to_append=}")
        binary_list = []
        for s in data:
            ascii_value = s
            binary = f"{ascii_value:08b}" 
            #binary = bin(ascii_value)[2:]
            #print(s, binary, type(binary))
            for bit in binary:
                binary_list.append(bit)
        length=len(binary_list)
        bits_to_append = -(length%6) + (6 if length%6 != 0 else 0)
        binary_list.extend([0]*bits_to_append)

        #print(f"{binary_list=}")

        base64 = []

        value = 0
        for index, bit in enumerate(reversed(binary_list)):
            #print (f"{bit=}")
            #converting block of 6 bits to integer value 
            value += ( 2**(index%6) if bit=='1' else 0)
            #print(f"{value=}")
            #print(bit, end = '')
            if (index+1)%6 == 0:
                base64.append(Base64Encoder.base64Encoding[value])
                #print(' ', end="")

                #resetting value
                value = 0
                pass
        #print()

        #padding if there is less bytes and returning the result
        return ''.join(reversed(base64))+''.join(['=']*bytes_to_append)

testEncoder.py

#!/usr/bin/env python3.10

from encoder import Base64Encoder

if __name__ == "__main__":
    print(Base64Encoder.encode("Hello"))
    print(Base64Encoder.encode("1 2 10 13 -7"))
    print(Base64Encoder.encode("A"))

    with open("image.jpg", "rb") as file_data:
        print(Base64Encoder.encode(file_data.read()))

Output:

$ ./testEncoder.py 
SGVsbG8=
MSAyIDEwIDEzIC03
QQ==

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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.

    Python code to convert to base64

    Image Source: Wikipedia

    Using 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:

    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:

    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

    How do you convert text to Base64 in Python?

    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.

    How do I convert to Base64?

    How to convert file to Base64 online.
    Select a local file from your computer..
    If necessary, select the desired output format..
    Press the “Encode file to Base64” button..
    Download or copy the result from the “Base64” field..

    How do you convert binary to Base64 in Python?

    “convert binary image data to base64 with python” Code Answer.
    >>> import base64..
    >>> encoded = base64. b64encode(b'data to be encoded').
    >>> encoded..
    b'ZGF0YSB0byBiZSBlbmNvZGVk'.
    >>> data = base64. b64decode(encoded).
    >>> data..
    b'data to be encoded'.

    How do I convert an Image to Base64 in Python?

    To convert the Image to Base64 String in Python, use the Python base64 module that provides b64encode() method. Python base64. b64encode() function encodes a string using Base64 and returns that string.