Remove the 0b in binary
Question:
I am trying to convert a binary number I have to take out the 0b string out.
I understand how to get a bin number
x = 17
print(bin(17))
'0b10001'
but I want to take the 0b
in the string out and I am having some issues with doing this. This is going to be within a function returning a binary number without the 0b
.
Answers:
Use slice operation to remove the first two characters.
In [1]: x = 17
In [2]: y = bin(x)[2:]
In [3]: y
Out[3]: '10001'
use python string slice
operation.
a = bin(17)
b = bin(17)[2:]
to format this to 8-bits use zfill
.
c = b.zfill(8)
It’s easy just make this function:
def f(n):print('{:0b}'.format(n))
f(17)
>>> 10001
print (bin(int(input().strip()))[2:])
Pythonic way to solve. 😉
I do not know why nobody suggested using lstrip
.
integer = 17
bit_string = bin(integer)
final = bit_string.lstrip('-0b') # minus to also handle negations
print(final) # prints 10001
bin(n).replace("0b", "")
This one is using replace
Where n
is the provided decimal
inhexa=(hexanum.get()) # gets the hexa value
dec = int(inhexa,16) #changes the base ensures conversion into base 16 to interger
b=bin(dec)[2:] #converts int/dec into binary and shows string except first two digits
with Python 3.6 you can use f-strings
print( f'{x:b}' )
'10001'
format(17, 'b')
>>> '10001'
Use the format()
builtin. It also works for hexadecimal, simply replace 'b'
with 'x'
.
Since this page will answer to developers performing byte handling therefore performance oriented there should be a benchmarked comparison of above methods.
Assuming we do not require padding (a subject this thread tackles) the aforementioned solutions (including the top answer from the other thread) yield these results (for 10 million random 21-bit integers) :
Results
Benchmark can be find here.
So the f'{x:’b’}’ proves faster with the intuitive slicing method as a close second (results were consistent between runs on a r5-3600 cpu and 16GB of 2133MHz 19CL system memory).
In the end the answer from Diego Roccia was the fastest and pretty elegant.
Padding with leading zeros and further options of said method can be found here but using the f'{x:’b’}’ with .zfill() for padding is faster than the solutions given there (find actual tests in here).
So the answer is:
f'{x:'b'}'
I am trying to convert a binary number I have to take out the 0b string out.
I understand how to get a bin number
x = 17
print(bin(17))
'0b10001'
but I want to take the 0b
in the string out and I am having some issues with doing this. This is going to be within a function returning a binary number without the 0b
.
Use slice operation to remove the first two characters.
In [1]: x = 17
In [2]: y = bin(x)[2:]
In [3]: y
Out[3]: '10001'
use python string slice
operation.
a = bin(17)
b = bin(17)[2:]
to format this to 8-bits use zfill
.
c = b.zfill(8)
It’s easy just make this function:
def f(n):print('{:0b}'.format(n))
f(17)
>>> 10001
print (bin(int(input().strip()))[2:])
Pythonic way to solve. 😉
I do not know why nobody suggested using lstrip
.
integer = 17
bit_string = bin(integer)
final = bit_string.lstrip('-0b') # minus to also handle negations
print(final) # prints 10001
bin(n).replace("0b", "")
This one is using replace
Where n
is the provided decimal
inhexa=(hexanum.get()) # gets the hexa value
dec = int(inhexa,16) #changes the base ensures conversion into base 16 to interger
b=bin(dec)[2:] #converts int/dec into binary and shows string except first two digits
with Python 3.6 you can use f-strings
print( f'{x:b}' )
'10001'
format(17, 'b')
>>> '10001'
Use the format()
builtin. It also works for hexadecimal, simply replace 'b'
with 'x'
.
Since this page will answer to developers performing byte handling therefore performance oriented there should be a benchmarked comparison of above methods.
Assuming we do not require padding (a subject this thread tackles) the aforementioned solutions (including the top answer from the other thread) yield these results (for 10 million random 21-bit integers) :
Results
Benchmark can be find here.
So the f'{x:’b’}’ proves faster with the intuitive slicing method as a close second (results were consistent between runs on a r5-3600 cpu and 16GB of 2133MHz 19CL system memory).
In the end the answer from Diego Roccia was the fastest and pretty elegant.
Padding with leading zeros and further options of said method can be found here but using the f'{x:’b’}’ with .zfill() for padding is faster than the solutions given there (find actual tests in here).
So the answer is:
f'{x:'b'}'