In Python 2.6 or later, use format syntax :
'{0:0=#10b}'.format(my_num)[2:]
One neat thing about Python strings is that they are sequences. If all you have to do is iterate over the characters, then there is no need to convert the string to a list.
Change . For steganography, you might be interested in converting a character stream to a bit stream. Here's how you could do it with generators:
def str2bits(astr):
for char in astr:
n=ord(char)
for bit in '{0:0=#10b}'.format(n)[2:]:
yield int(bit)
And to convert the bitstream back to a character stream:
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
return itertools.izip_longest(*[iter(iterable)]*n,fillvalue=fillvalue)
def bits2str(bits):
for b in grouper(8,bits):
yield chr(int(''.join(map(str,b)),2))
For example, you could use the above functions as follows:
for b in str2bits('Hi Zvarberg'):
print b,
print ''.join([c for c in bits2str(str2bits('Hi Zvarberg'))])
, SO Ned Batchelder , , Python PIL . .
, ( Python), numpy.