binary to decimal conversion

wheineman 1wheinemanNOwhSPAM at uconect.net.invalid
Sun Mar 19 17:41:00 EST 2000


Okay, here my decimal to string representing binary value. I
append a lowercase b to the end to make it more readable (for me)
and you can get the number of bits by taking the len() - 1.

def bin(num):
    """
    Returns a string binary representation of a number. For
example
    bin(255) returns '11111111b'.

    """
    returnValue = 'b'
    mask = 0
    i = 0
    while(1):
        mask = 1 << i
        if (i != 0) and (mask > num):
            break
        if (mask & num):
            returnValue = '1' + returnValue
        else:
            returnValue = '0' + returnValue
        i = i + 1

    return returnValue



Cheers,

Willy

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!




More information about the Python-list mailing list