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 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) # Deccoding 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
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 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) # Deccoding 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
Recommended Posts:
- Python | Convert String to Binary
- Python Program to Convert any Positive Real Number to Binary string
- Python | Convert List of String List to String List
- Python | Check if a given string is binary string or not
- Python program to convert floating to binary
- Python program to convert decimal to binary number
- Python | Ways to convert hex into binary
- Python - Convert Binary tuple to Integer
- Python | Program to convert String to a List
- Python | Convert a list of characters into a string
- Python | Convert Bytearray to Hexadecimal String
- Python | Convert String to tuple list
- Python | Program to convert a tuple to a string
- Python program to convert a list to string
- Python | Convert string to DateTime and vice-versa
- Python | Convert list of string to list of list
- Python | Convert dictionary object into string
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert a string representation of list into list
- Python | Convert list of string into sorted list of integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

