How to convert binary string to and from ascii text in python

In this tutorial, we will see How to convert any binary string to and from ASCII text in Python. ASCII stands for American Standard Code For Information Interchange. It is a standard 8-bit encoding format which assigns numerical values to other characters in computer such as letters, punctuation marks etc.

The ASCII value of ‘K’ is 75. You can check the ASCII value of different characters by executing the code given below. It takes an input character from the user and displays its ASCII value using ord() function.

# Program to find the ASCII value of the given character
char = str(input("Enter any character: "))
print("The ASCII value of '" + char + "' is", ord(char))
Enter any character: a
The ASCII value of 'a' is 97

The ord() function only works for a character. If you want to obtain ASCII value of all characters in a string then use for loop to access all elements one by one. Convert them into ascii using ord() function and append the value in another variable.

Computers store data in the form of binary numbers i.e. 1’s and 0’s. Suppose you want to perform some operations or manipulate a string which is stored in the computer memory. For this, you need to convert this binary string to ASCII value to retrieve the original string. This article discusses different ways to convert a binary string into ASCII value using Python language. If you want to learn more about Python Programming, visit Python Programming Tutorials.

the common ways to convert binary strings to and from ASCII text in Python are:

  1. Use the binascii module to convert Binary String to ASCII  
  2. Binary String to ASCII using  Int.to_bytes() function
  3. ASCII to Binary String using Int.from_bytes() function

Converting binary string or text to ASCII using binascii module

Binascii module aid in conversion of binary string to their equivalent ASCII representation. First of all, import the library of binascii and then take binary string as an input from the user. You can also convert a string to binary representation by inserting “b” at the start of an input string. b2a.uu() is a binascii function which converts the binary string to ascii representation.

import binascii

# Initializing a binary string
Text = b"This is my string"

# Calling the b2a_uu() function to
# Convert the binary string to ascii
ASCII = binascii.b2a_uu(Text)

# Getting the ASCII equivalent
b"15&AI<R!I<R!M>2!S=')I;F< \n"

Converting binary String to ASCII using int.to_ byte() function

First step is to initialize a binary string using int(binary_input, base) command. Pass the string of 0s and 1s in the first argument and base of the number system in second argument. Before moving towards coding, lets first understand how a binary string composed of bits of 0s and 1s is converted into an ASCII value. You already know eight bits are equal to 1 byte. Suppose you have an binary string as shown below. In order to find ascii value, we have first grouped 8 bits. A group of 8 bits represent 1 byte which represents 1 charachter.

First step is to find the number of bytes in a binary string which is done using (input_string.bit_length() +7) // 8 command. Here, input_string.bit_length() returns the total number of bits in an input string. After this, convert it into ASCII text using decoder() function. The complete code is given below.

# Initialize a binary string
input_string=int("0100100001100101011011000110110001101111", 2);

#Obtain the total number of bytes
Total_bytes= (input_string.bit_length() +7) // 8

#Convert these bits to bytes
input_array = input_string.to_bytes(Total_bytes, "big")

#Convert the bytes to an ASCII value and display it on the output screen

Converting ASCII to Binary Text using Int.from_bytes() function

The above two methods were related to the conversion of binary to ASCII. In this method, we will learn to convert ASCII to Binary. For this, convert the string into an array using string.encode() function. Then call the function int.from_byte() to convert the array of bytes into binary integer which is then passed to bin() function to obtain a binary string of 0s nd 1s..

input_array = "Hello".encode()
binary_array= int.from_bytes(input_array, "big")
output_string = bin(binary_array)

This tutorial is all about conversion of binary from ASCII and vice versa. Similarly, you can also convert other number systems such as decimal numbers, hexadecimal numbers into binary numbers and vice versa. If you have any feedback about this article, do let us know. To learn more about python language, visit this link.

Leave a Comment

Your email address will not be published. Required fields are marked *