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.

askedAug 19, 2013 at 10:25
James Clarke's user avatar
2
  • 0b11001010 is justsyntactic sugar, a different way to spell an integer number. Both0b11001010 and202 mean the same thing, an integer value of 202.CommentedAug 19, 2013 at 10:31
  • You canprint the value ofNewNumber as binary, but it won't bestored using that notation.CommentedAug 19, 2013 at 10:34

1 Answer1

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:

>>> 0b11001010202

The same thing will happen with octal and hexadecimal values. So you can convert your binary string to an integer, with theint() function'sbase 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 builtinbin() function:

>>> bin(202)0b11001010
answeredAug 19, 2013 at 10:26
Peter Varo's user avatar
Sign up to request clarification or add additional context in comments.

6 Comments

The output is still a string.. You could justprint(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?
|

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.