0

So I have this operation in pythonx = int(v,base=2) which takesvas a Binary String. What would be the inverse operation to that?For example, given1101000110111111011001100001 it would return219936353, so I want to get this binary string from the219936353 number.Thanks

Mangu Singh Rajpurohit's user avatar
Mangu Singh Rajpurohit
11.5k4 gold badges76 silver badges101 bronze badges
askedNov 18, 2015 at 9:02
lpares12's user avatar
0

3 Answers3

4

Try out the bin() function.

bin(yourNumber)[2:]

will give you string containing bits for your number.

ifloop's user avatar
ifloop
8,4162 gold badges28 silver badges35 bronze badges
answeredNov 18, 2015 at 9:06
Mangu Singh Rajpurohit's user avatar
Sign up to request clarification or add additional context in comments.

9 Comments

thanks! If I operate with this string the0b from the start won't interfere with the operation, right?
What exact operation, do you want to perform ?
@deuseux12 You can useprint bin(219936353)[2:].zfill(8) to remove that0b.
okey. That binary string is the conversion from the word "hola" to binary. So I would have to convert the binary string to the word. If it gives any problem I'll use Borja solution to take the0b out. Thanks
@deuseux12, This doesn't work:int(bin(1), 10). See my answer for the first correct solution:int('{:b}'.format(1), 10) =>1.
|
0
>>> bin(219936353)'0b1101000110111111011001100001'
answeredNov 18, 2015 at 9:09
11thdimension's user avatar

1 Comment

Mind toexplain your solution a bit?
0
num = 219936353print("{:b}".format(num))--output:--1101000110111111011001100001

The other solutions are all wrong:

num = 1string = bin(1)result = int(string, 10)print(result)--output:--Traceback (most recent call last):  File "1.py", line 4, in <module>    result = int(string, 10)ValueError: invalid literal for int() with base 10: '0b1'

You would have to do this:

num = 1string = bin(1)result = int(string[2:], 10)print(result)  #=> 1
answeredNov 18, 2015 at 9:08
7stud's user avatar

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.