views:

128

answers:

2

I need to convert from an integer to a list of size 8 that is the binary representation of that number (number <= 255) and back. Currently I am using these lines

list(bin(my_num)[2:].rjust(8,'0'))
int("".join(my_list),2)

I did some googling, but had a hard time finding relevant information. I'm just curious if there is a faster, or more standard way to do this.

edit: Would using bit masking make it faster. E.g. something like this

[(my_num>>y)&1 for y in xrange(7,-1,-1)]

Like I mentioned in a comment I am using this for a steganography app I am writing, so I am doing this thousands of times (3 times per pixel in an image), so speed is good.

+3  A: 

You could use zfill instead of rjust.

list(bin(my_num)[2:].zfill(8))
recursive
Thanks, I missed this function in my searching.
ZVarberg
I tried using this line in my app replacing the bit masking solution I posted above, and it gave me an average improvement of about 20%. zfill gave an improvement of about 2% on average over the rjust method as well, but an improvement of 12% in the worst case. Of course all that data is for my specific app so I don't know if it's universal, but it's good enough for me!
ZVarberg
+4  A: 

In Python 2.6 or newer, use format syntax:

'{0:0=#10b}'.format(my_num)[2:]
# '00001010'

One of the neat things about Python strings is that they are sequences. If all you need to do is iterate through the characters, then there is no need to convert the string to a list.

Edit: For steganography, you might be interested in converting a stream of characters into a stream of bits. Here is how you could do that 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 a stream of bits back into a stream of characters:

def grouper(n, iterable, fillvalue=None):
    # Source: http://docs.python.org/library/itertools.html#recipes
    "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 like this:

for b in str2bits('Hi Zvarberg'):
    print b,
# 0 1 0 0 1 0 0 0 0 1 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 0 1 1 0 0 1 1 0 0 0 0 1 0 1 1 1 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 1 1 0 0 1 0 0 1 1 0 0 1 1 1

# To show bits2str is the inverse of str2bits:
print ''.join([c for c in bits2str(str2bits('Hi Zvarberg'))])
# Hi Zvarberg

Also, SO guru Ned Batchelder does some steganography-related experiments using Python and PIL here. You may be able to find some useful code there.

If you find you need more speed (and still want to code this in Python), you may want to look into using numpy.

unutbu
Yes, but in order to change an element I need a list correct? Strings are immutable.
ZVarberg
Thanks for the pointers to the Steganography stuff. I try to avoid looking at other Steganography articles in python because I want this app to be my app. I'm afraid if I look at other peoples solutions, their ideas will slip into my code without me finding my own solutions. I have a working app and it works pretty well, I'm just trying to make some improvements.
ZVarberg