简体   繁体   中英

Is there a better way to convert from decimal to binary in python?

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. Eg 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.

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 .

您可以使用zfill代替rjust

list(bin(my_num)[2:].zfill(8))

Here is one method for decimal to binary conversion:

  • divide the decimal number by 2
  • take the remainder and record it in on the side
  • divide the quotient by 2
  • repeat until the decimal cannot be divided further
  • record the remainder in reverse order and you get the resultant binary number

Which may be coded as:

d=int(raw_input("enter your decimal:"))
l=[]
while d>0:
    x=d%2
    l.append(x)
    d=d/2
l.reverse()
for i in l:
    print i,
print " is the decimal representation of givin binary data."

First solution :
A fast method must not use loops.
I would recommend using a lookup table that you would build once and use as often you wish.

tb = []
for i in range(256):
  tb.append( f"{i:08b}")
# once build you can use it for the whole image.
print( tb[27]) # will print: 00011011

Second solution :
but if you're really serious about speed, you should not use characters. You should load your image in a bytearray (it is mutable) and directly modify bits in pixel.

img[2][8] |≃ 0b10 # set second bit from right
img[2][8] |= 1<<1 # same
img[2][8] &= ~0b10 # reset second bit
img[2][8] &= ~1<<1 # same
img[2][8] ^= 0b1010  # invert second and fourth bits

I have given here program for decimal to binary conversion.

print "Program for Decimal to Binary Conversion"

n = 0
bin = 0
pos = 1

print "Enter Decimal Number:", 
n = input()

while(n > 0):
   bin = bin + (n % 2) * pos;
   n = n / 2;
   pos *= 10;

print "The Binary Number is: ", bin       

#sample output
#Program for Decimal to Binary Conversion
#Enter Decimal Number: 10
#The Binary Number is: 1010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM