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 = 0b11001010is there a really simple way and I am overlooking it?
Thanks.
0b11001010is justsyntactic sugar, a different way to spell an integer number. Both0b11001010and202mean the same thing, an integer value of 202.Martijn Pieters– Martijn Pieters2013-08-19 10:31:39 +00:00CommentedAug 19, 2013 at 10:31- You canprint the value of
NewNumberas binary, but it won't bestored using that notation.Martijn Pieters– Martijn Pieters2013-08-19 10:34:37 +00:00CommentedAug 19, 2013 at 10:34
1 Answer1
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:
>>> 0b11001010202The 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)202After 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)0b110010106 Comments
print(Number) and be done with it.Explore related questions
See similar questions with these tags.
