Python program to convert ASCII to Binary
Last Updated :
08 Feb, 2024
In this article, we are going to discuss the conversion of ASCII to Binary in the Python programming language.
Method 1: Using binascii module
Binascii helps convert between binary and various ASCII-encoded binary representations.
a2b_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 a2b_uu() function is used to convert the specified ASCII format to its corresponding binary equivalent.
Syntax: a2b_uu(Text)
Parameter: This function accepts a single parameter which is illustrated below:
- Text: This is the specified ASCII string that is going to be converted into its binary equivalent.
Return Values: This function returns the binary equivalent.
Example: Convert ASCII to binary using Python
Python3
import binascii
Text = "21T9'(&ES(&$@0U,@4&]R=&%L"
Binary = binascii.a2b_uu(Text)
print (Binary)
|
Output:
b'GFG is a CS Portal'
Time Complexity: O(logn)
Space Complexity: O(n)
Method 2: Using Built-in Types
Firstly, call string.encode() function to turn the specified string into an array of bytes and then call int.from_bytes(byte_array, byte_order) with byte_order as “big” to convert the byte_array into a binary integer. Finally, call bin(binary_int) to convert binary_int to a string of binary characters.
Example: Convert ASCII to binary using Python
Python3
byte_array = "GFG" .encode()
binary_int = int .from_bytes(byte_array, "big" )
binary_string = bin (binary_int)
print (binary_string)
|
Output:
0b10001110100011001000111
The Time and Space Complexity of all the methods is :
Time Complexity: O(logn)
Space Complexity: O(n)
Method #3 – using ord(), zfill() and bin() method –
In this approach we will simply use the ord() method alongside the bin() method to convert the string into binary. We will do it characterwise and later we will join it together
Step – 1 : Firstly we will define a function which takes the string as input.
Step – 2 : Then we will use the ord() and bin() method together to first convert the character into it’s ASCII value and then convert that ASCII into binary.
Step – 3: As the bin() method of Python converts into binary and starts the result with “0b” we will consider values starting after the”b”, generally the binary of any character is of length 8 (including the 0b part), so as we are omitting the 0b part we will use the zfill() method to make those results of length 8.
Step – 4 : As we are converting character by character the result will be a list in which each element will be the binary equivalent of each character. So to print them together we will use the join() method.
Below is the Implementation –
Python3
def string_to_binary(st : str ):
return [ bin ( ord (i))[ 2 :].zfill( 8 ) for i in st]
def binary_to_string(bits):
return ''.join([ chr ( int (i, 2 )) for i in bits])
st = "GFG"
b = string_to_binary(st)
bt = "".join(b)
print ( "Converting the String into Binary : " ,bt)
s = binary_to_string(b)
print ( "The string created from the binary parts : " ,s)
|
Output
Converting the String into Binary : 010001110100011001000111
The string created from the binary parts : GFG
Time Complexity – O(n) # n is the length of the string
Auxiliary Space – O(n) #n is the length of the list that will form
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...