Open In App

Convert binary to string using Python

Last Updated : 05 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Data conversion has always been widely used utility and one among them can be the conversion of a binary equivalent to its string. 
Let’s discuss certain ways in which this can be done.

Method #1:
The naive approach is to convert the given binary data in decimal by taking the sum of binary digits (dn) times their power of 2*(2^n). The binary data is divided into sets of 7 bits because this set of binary as input, returns the corresponding decimal value which is ASCII code of the character of a string. This ASCII code is then converted to string using chr() function.
Note: Here we do slicing of binary data in the set of 7 because, the original ASCII table is encoded on 7 bits, therefore, it has 128 characters.

Python3




# Python3 code to demonstrate working of
# Converting binary to string
# Using BinarytoDecimal(binary)+chr()
  
 
# Defining BinarytoDecimal() function
def BinaryToDecimal(binary):
        
    binary1 = binary
    decimal, i, n = 0, 0, 0
    while(binary != 0):
        dec = binary % 10
        decimal = decimal + dec * pow(2, i)
        binary = binary//10
        i += 1
    return (decimal)   
 
# Driver's code
# initializing binary data
bin_data ='10001111100101110010111010111110011'
  
# print binary data
print("The binary value is:", bin_data)
  
# initializing a empty string for
# storing the string data
str_data =' '
  
# slicing the input and converting it
# in decimal and then converting it in string
for i in range(0, len(bin_data), 7):
     
    # slicing the bin_data from index range [0, 6]
    # and storing it as integer in temp_data
    temp_data = int(bin_data[i:i + 7])
      
    # passing temp_data in BinarytoDecimal() function
    # to get decimal value of corresponding temp_data
    decimal_data = BinaryToDecimal(temp_data)
      
    # Decoding the decimal value returned by
    # BinarytoDecimal() function, using chr()
    # function which return the string corresponding
    # character for given ASCII value, and store it
    # in str_data
    str_data = str_data + chr(decimal_data)
  
# printing the result
print("The Binary value after string conversion is:",
       str_data)


Output

The binary value is: 10001111100101110010111010111110011
The Binary value after string conversion is:  Geeks

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using int() function 
Whenever an int() function is provided with two arguments, it tells the int() function that the second argument is the base of the input string. If the input string is larger than 10, then Python assumes that the next sequence of digits comes from ABCD… .Therefore, this concept can be used for converting a binary sequence to string. Below is the implementation.
 

Python3




# Python3 code to demonstrate working of
# Converting binary to string
# Using BinarytoDecimal(binary)+chr()
  
 
# Defining BinarytoDecimal() function
def BinaryToDecimal(binary):
     
    # Using int function to convert to
    # string  
    string = int(binary, 2)
     
    return string
     
# Driver's code
# initializing binary data
bin_data ='10001111100101110010111010111110011'
  
# print binary data
print("The binary value is:", bin_data)
  
# initializing a empty string for
# storing the string data
str_data =' '
  
# slicing the input and converting it
# in decimal and then converting it in string
for i in range(0, len(bin_data), 7):
     
    # slicing the bin_data from index range [0, 6]
    # and storing it in temp_data
    temp_data = bin_data[i:i + 7]
      
    # passing temp_data in BinarytoDecimal() function
    # to get decimal value of corresponding temp_data
    decimal_data = BinaryToDecimal(temp_data)
      
    # Decoding the decimal value returned by
    # BinarytoDecimal() function, using chr()
    # function which return the string corresponding
    # character for given ASCII value, and store it
    # in str_data
    str_data = str_data + chr(decimal_data)
 
# printing the result
print("The Binary value after string conversion is:",
       str_data)


Output

The binary value is: 10001111100101110010111010111110011
The Binary value after string conversion is:  Geeks

Time Complexity: O(n)
Auxiliary Space: O(n)



Similar Reads

Python | Convert String to Binary
Data conversion have always been widely used utility and one among them can be conversion of a string to it's binary equivalent. Let's discuss certain ways in which this can be done. Method #1 : Using join() + ord() + format() The combination of above functions can be used to perform this particular task. The ord function converts the character to
4 min read
Python Program to Convert any Positive Real Number to Binary string
Given any Real Number greater than or equal to zero that is passed in as float, print binary representation of entered real number. Examples: Input: 123.5 Output: 1 1 1 1 0 1 1 . 1 Input: 0.25 Output: .01 Mathematical Logic along with steps done in programming: Any real number is divided into two parts: The integer part and the Fraction part. For b
4 min read
Convert image to binary using Python
In this article, we are going to convert the image into its binary form. A binary image is a monochromatic image that consists of pixels that can have one of exactly two colors, usually black and white. Binary images are also called bi-level or two-level. This means that each pixel is stored as a single bit—i.e., 0 or 1. The most important library
1 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Convert Unicode String to a Byte String in Python
Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular string. In this article, we will explore five diff
2 min read
Convert Each Item in the List to String using Python
Working with lists in Python is a common task, and there are various scenarios where you might need to convert each item in a list to a string. In this article, we will explore different methods to achieve this using list comprehension, the map() function, the enumerate function, and recursion. Example : Input : ['I', 'want', 4, 'apples', 'and', 18
3 min read
Python program to convert floating to binary
Python doesn't provide any inbuilt method to easily convert floating point decimal numbers to binary number. So, Let’s do this manually. Approach : To convert a floating point decimal number into binary, first convert the integer part into binary form and then fractional part into binary form and finally combine both results to get the final answer
3 min read
Python | Ways to convert hex into binary
Conversion of hex to binary is a very common programming question. In this article, we will see a few methods to solve the above problem. Method #1: Using bin and zfill C/C++ Code # Python code to demonstrate # conversion of a hex string # to the binary string # Initialising hex string ini_string = "1a" scale = 16 # Printing initial strin
2 min read
Python - Convert Binary tuple to Integer
Given Binary Tuple representing binary representation of a number, convert to integer. Input : test_tup = (1, 1, 0) Output : 6 Explanation : 4 + 2 = 6. Input : test_tup = (1, 1, 1) Output : 7 Explanation : 4 + 2 + 1 = 7. Method #1 : Using join() + list comprehension + int() In this, we concatenate the binary tuples in string format using join() and
5 min read
Python program to convert Base 4 system to binary number
Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1: Take an empty string say resultstr.We convert the
3 min read
Practice Tags :