Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is binascii.b2a_base64 in Python?

Sarvech Qadir
widget

The binascii is a widely used Python library for ASCII-encoded binary representations. It contains several methods for converting to binary from ASCII or hex, and vice versa.

binascii.b2a_base64 is a widely used method in Python binascii to convert a binary data to a line of ASCII characters. However, these conversions are made in base64 codingBase64 coding refers to encoding data from binary to text. This translates binary data to a radix-64 format in order to represent binary data in an ASCII string format..

Syntax

binascii.b2a_base64 (data, newline: bool=True)

Parameters

  1. data: The binary data that needs to be converted. Returns a converted line of ASCII characters.
  2. newline: Adds a newline “\n” if the newline variable is set to True.

Return value

Returns str|bytes

Code

Let’s look at a basic example using b2a_base64. In this code, we simply convert data to a line of ASCII characters in base64 coding. First, we convert data into binary format using bytes method. Next, we use binascii.b2a_base64 to convert the data and print the result.

import binascii
# data
x = "hello world"
# convert to binary
res = bytes(x, 'utf-8')

# ASCII in base_64
print(binascii.b2a_base64(res))

RELATED TAGS

python

CONTRIBUTOR

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring