Convert binary to string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Convert binary to string

How can i convert binary to string in python? I wanna decode this binary to string : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011

25th Mar 2022, 9:04 AM
Amir javadikia
Amir javadikia - avatar
2 Answers
+ 7
You got it from GeeksforGeeks right? Let i = 0 Let result = "" #empty string While <i> less than length of string Slice 8 characters off the string, From offset <i> to <i> + 8 Convert that chunk into an integer using int function Specify 2 (binary) as conversion base Convert the integer to a character using chr function Push the character to <result> Increment <i> by 8 Print <result>
25th Mar 2022, 9:29 AM
Ipang
+ 2
Amir javadikia , we can use the int() conversion, since it accepts an optional argument that describes the number format of the main argument. using 2 means binary format. my_bin = "01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011" print(int(my_bin, 2)) # or res = int(my_bin, 2) print(res)
25th Mar 2022, 12:11 PM
Lothar
Lothar - avatar