In Ruby i do so
asd = 123
asd = '%b' % asd # => "1111011"
In Ruby i do so
asd = 123
asd = '%b' % asd # => "1111011"
you can also do string formatting, which doesn't contain '0b':
>>> '{:b}'.format(123) #{0:b} in python 2.6
'1111011'
'{0:b}'.format(123), and it is for python version ≥ 2.6 only.'{:b}' is python 2.7 and 3.1 version.in Python >= 2.6 with bin():
asd = bin(123) # => '0b1111011'
To remove the leading 0b you can just take the substring bin(123)[2:].
bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. Ifxis not a Python int object, it has to define an__index__()method that returns an integer.New in version 2.6.
lstrip instead of strip as the latter will strip any 0s from the end of the string as well as the front (thereby changing the value). The slice is probably a better way to go, as it will only ever remove those two characters from the string.