November 1st, 2009 Add Your Comments

The following will produce the bit representation of an ord(byte) in python

def bit(chr, i=0, str=''):
    if i > 7: return str
    coef = (2**(8-i-1))
    if chr >= coef: return bit(chr-coef, i+1, str+'1')
    return bit(chr, i+1, str+'0')

Example

print bit(ord(“a”))
01100001