0

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.

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by vegaseat
0

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.