def unbits(s, endian='big'):
 if (endian == 'little'):
  u = (lambda s: chr(int(s[::(-1)], 2)))
 elif (endian == 'big'):
  u = (lambda s: chr(int(s, 2)))
 else:
  raise ValueError("unbits(): 'endian' must be either 'little' or 'big'")
 out = ''
 cur = ''
 for c in s:
  if (c in ['1', 1, True]):
   cur += '1'
  elif (c in ['0', 0, False]):
   cur += '0'
  else:
   raise ValueError(('unbits(): cannot decode the value %r into a bit' % c))
  if (len(cur) == 8):
   out += u(cur)
   cur = ''
 if cur:
  out += u(cur.ljust(8, '0'))
 return ''.join(out)

def unbits ( s , endian = 'big' ) : if ( endian == 'little' ) : u = ( lambda s : chr ( int ( s [ : : ( - 1 ) ] , 2 ) ) ) elif ( endian == 'big' ) : u = ( lambda s : chr ( int ( s , 2 ) ) ) else : raise ValueError ( "unbits(): 'endian' must be either 'little' or 'big'" ) out = '' cur = '' for c in s : if ( c in [ '1' , 1 , True ] ) : cur += '1' elif ( c in [ '0' , 0 , False ] ) : cur += '0' else : raise ValueError ( ( 'unbits(): cannot decode the value %r into a bit' % c ) ) if ( len ( cur ) == 8 ) : out += u ( cur ) cur = '' if cur : out += u ( cur . ljust ( 8 , '0' ) ) return '' . join ( out )
unbits -> str converts an iterable of bits into a string .