+ 1
Convert binary to string
How can i convert binary to string in python? I wanna decode this binary to string : 01000111011001010110010101101011011100110110011001101111011100100100011101100101011001010110101101110011
2 odpowiedzi
+ 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>
+ 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)