major typo nastiness in dec2bin

Jeff Pinyan jeffp at crusoe.net
Mon Mar 27 13:22:19 EST 2000


Sorry for that atrocious dec2bin().  Here's a nicer, working one:

def dec2bin (val):
  val = "%o" % val
  ret = ""    
  l = len(val)  
  for i in range(l):
    byte = int(val[l-i-1])
    bit = 0     

    if byte & 1: bit = 1
    if byte & 2: bit = 10 + bit
    if byte & 4: bit = 100 + bit

    if i != len(val) - 1: bit = "%03d" % bit
    else: bit = `bit`

    ret = bit + ret
  return ret

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy at pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/




More information about the Python-list mailing list