9

I have the variable Number which is equal to "0b11001010" and I want it to be the type int like a normal binary is stored e.g. 0b11001010

Number = "0b11001010"
NewNumber = 0b11001010

is there a really simple way and I am overlooking it?

Thanks.

2
  • 0b11001010 is just syntactic sugar, a different way to spell an integer number. Both 0b11001010 and 202 mean the same thing, an integer value of 202. Commented Aug 19, 2013 at 10:31
  • You can print the value of NewNumber as binary, but it won't be stored using that notation. Commented Aug 19, 2013 at 10:34

1 Answer 1

22

In python you can only create it as a binary value (as a syntactic sugar), it will be converted into an integer immediately. Try it for yourself:

>>> 0b11001010
202

The same thing will happen with octal and hexadecimal values. So you can convert your binary string to an integer, with the int() function's base argument like:

>>> int('0b11001010', 2)
202

After the conversion you can do any operations on it -- just like with an integer, since it is an integer.

Of course you can convert it back at any time to a binary string, with the builtin bin() function:

>>> bin(202)
0b11001010
Sign up to request clarification or add additional context in comments.

6 Comments

The output is still a string.. You could just print(Number) and be done with it.
If I do, type(bin(int(Number, 2))) it comes up as string, and the function I send this number off to can only accept an int in binary form :/. I can send a little code if you want.
@JamesClarke please send!
So the function is along the lines of Send(Code, Addr, Data) The Data value is the Binary in question, Addr is a hex location of the Microchip on a board connected on a raspberry Pi, This info then gets sent to the function SendValue() where it toggles a clk and then sends the info and toggles it back. The Data can be Binary or Hex. If you need more info just ask.
@JamesClarke please try it with the converted int value, and let's see what happens. What is the error if any?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.