Open In App

Python program to convert binary to ASCII

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Method 1: By using binascii module 

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

b2a_uu() function: Here the “uu” stands for “UNIX-to-UNIX encoding” which takes care of the data conversion from strings to binary and ASCII values according to the specified program.

The b2a_uu() function is used to convert the specified binary string to its corresponding ASCII equivalent.

Syntax: b2a_uu(Text)

Parameter: This function accepts a single parameter which is illustrated below:

  • Text: This is the specified binary string that is going to be converted into its ASCII equivalent.

Return Values: This function returns the ASCII equivalent.

Example: Convert binary to ASCII.

Python3




# Python program to illustrate the
# conversion of Binary to ASCII
 
# Importing binascii module
import binascii
 
# Initializing a binary string
Text = b"GFG is a CS Portal"
 
# Calling the b2a_uu() function to
# Convert the binary string to ascii
Ascii = binascii.b2a_uu(Text)
 
# Getting the ASCII equivalent
print(Ascii)


Output:

b"21T9'(&ES(&$@0U,@4&]R=&%L\n"

Method 2: Using Built-in Types.

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)

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

In this approach, the idea is to use the chr() and join() methods to convert binary to ASCII values. Below are the steps:

  1. Declare a function that takes the binary value as an argument. 
  2. Then, use the chr() method to convert each of the bits passed into its ASCII value by passing the chr() method an integer value whose base is 2 i.e., in binary.
  3. The int(i, 2) part will convert each character into the ASCII value, and then the chr() method will convert that ASCII value into a proper string.
  4. Now, use the join method to join the results, as the binary equivalent of each character as a list, and print the result.

Below is the implementation of the above approach:

Python3




# Function to convert binary to ASCII value
def binary_to_string(bits):
    return ''.join([chr(int(i, 2)) for i in bits])
   
# Driver Code
 
# This is the binary equivalent of string "GFG"
bin_values = ['01000111', '01000110', '01000111']
 
# calling the function
# and storing the result in variable 's'
s = binary_to_string(bin_values)
 
# Printing the result
print("The string created from the binary parts : ",s)


Output

The string created from the binary parts :  GFG

Time Complexity – O(n)

Space Complexity –O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads