Convert binary to string using Python - GeeksforGeeks (2024)

Last Updated : 05 Dec, 2022

Summarize

Comments

Improve

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: 10001111100101110010111010111110011The 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: 10001111100101110010111010111110011The Binary value after string conversion is: Geeks

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



Please Login to comment...

Convert binary to string using Python - GeeksforGeeks (2024)
Top Articles
The Assessment Process - Santa Clara University
List of Toughest Exams in India 2024
Patreon, reimagined — a better future for creators and fans
Regal Amc Near Me
Shs Games 1V1 Lol
Polyhaven Hdri
Plus Portals Stscg
Richard Sambade Obituary
Nordstrom Rack Glendale Photos
Tap Tap Run Coupon Codes
Flights to Miami (MIA)
Jefferson County Ky Pva
Jasmine
What Was D-Day Weegy
Learn How to Use X (formerly Twitter) in 15 Minutes or Less
Everything You Need to Know About Holly by Stephen King
Alaska: Lockruf der Wildnis
Ostateillustrated Com Message Boards
Check From Po Box 1111 Charlotte Nc 28201
Wausau Marketplace
How To Level Up Roc Rlcraft
Sizewise Stat Login
SuperPay.Me Review 2023 | Legitimate and user-friendly
Company History - Horizon NJ Health
Like Some Annoyed Drivers Wsj Crossword
Form F-1 - Registration statement for certain foreign private issuers
What Are The Symptoms Of A Bad Solenoid Pack E4od?
1979 Ford F350 For Sale Craigslist
Malluvilla In Malayalam Movies Download
Cable Cove Whale Watching
Garden Grove Classlink
Craigslist Efficiency For Rent Hialeah
Dailymotion
Wheeling Matinee Results
Rush County Busted Newspaper
Prévisions météo Paris à 15 jours - 1er site météo pour l'île-de-France
Ilabs Ucsf
A Grade Ahead Reviews the Book vs. The Movie: Cloudy with a Chance of Meatballs - A Grade Ahead Blog
Unm Hsc Zoom
Wow Quest Encroaching Heat
Why The Boogeyman Is Rated PG-13
Elizaveta Viktorovna Bout
10 games with New Game Plus modes so good you simply have to play them twice
Kornerstone Funeral Tulia
Froedtert Billing Phone Number
Miami Vice turns 40: A look back at the iconic series
Cabarrus County School Calendar 2024
Gary Vandenheuvel Net Worth
Costco The Dalles Or
Craiglist.nj
Michaelangelo's Monkey Junction
Vcuapi
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6236

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.