How to use Built-in Types. In Python

Here we will use a built-in type to convert binary to ASCII value.

Firstly, call int(binary_sting, base) with the base as 2 indicating the binary string. and then call int.to_bytes(byte_number, byte_order) function, where byte_order is taken as “big” and byte_number is taken as the number of bytes that binary_int occupies to return an array of bytes. This byte_number can be found using the operation binary_int.bit_length() + 7 // 8. And then call array.decode operation to turn the array into ASCII text.

Example: Convert binary to ASCII

Python3




# Python program to illustrate the
# conversion of Binary to ASCII
 
# Initializing a binary string in the form of
# 0 and 1, with base of 2
binary_int = int("11000010110001001100011", 2);
 
# Getting the byte number
byte_number = binary_int.bit_length() + 7 // 8
 
# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")
 
# Converting the array into ASCII text
ascii_text = binary_array.decode()
 
# Getting the ASCII value
print(ascii_text)


Output:

abc

The Time and Space Complexity of all the methods is :

Time Complexity: O(logn)

Space Complexity: O(n)

Python program to convert binary to ASCII

In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below:

Similar Reads

Method 1: By using binascii module

Binascii helps convert between binary and various ASCII-encoded binary representations....

Method 2: Using Built-in Types.

...

Method 3 : Using chr() and join() methods

Here we will use a built-in type to convert binary to ASCII value....