I'm trying to convert decimal to binary and I'm having a lot of trouble (new to python).
I was wondering why that won't work. Thanks.
EDIT: Argh, I'm so sorry but I got it fixed. It turns out that the assigned variables are on the wrong side.
I'm trying to convert decimal to binary and I'm having a lot of trouble (new to python).
I was wondering why that won't work. Thanks.
EDIT: Argh, I'm so sorry but I got it fixed. It turns out that the assigned variables are on the wrong side.
Most commonly used function ...
def int2bin(n):
'''convert denary integer n to binary string bStr'''
bStr = ''
if n < 0: raise ValueError, "must be positive"
if n == 0: return '0'
while n > 0:
bStr = str(n % 2) + bStr
n = n >> 1
return bStr
print int2bin(255) # 11111111
print int2bin(5000) # 1001110001000
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.