Convert to binary and store leading zeros in Python

I am trying to convert an integer to binary using the bin () function in Python. However, it always removes the leading zeros that I really need, so the result is always 8-bit:

Example:

bin(1) -> 0b1 # What I would like: bin(1) -> 0b00000001 

Is there any way to do this?

+85
python bitwise-operators formatting binary
Jun 04 '13 at 19:41
source share
8 answers

Use the format() function :

 >>> format(14, '#010b') '0b00001110' 

The format() function simply formats the input following the Format Specification> mini-language . In # format includes the prefix 0b , and size 010 formats the output according to a width of 10 characters with 0 padding; 2 characters for the prefix 0b , the remaining 8 for binary digits.

This is the most compact and direct option.

If you put the result on a large line, use str.format() and put the second argument to the format() function after the colon placeholder {:..} :

 >>> 'The produced output, in binary, is: {:#010b}'.format(14) 'The produced output, in binary, is: 0b00001110' 

If you don't need the 0b prefix, just release # and adjust the field length:

 >>> format(14, '08b') '00001110' 
+108
Jun 04 '13 at 19:54
source share
 >>> '{:08b}'.format(1) '00000001' 

See: Specification format Mini-language




Note for Python 2.6 or later, you cannot omit the identifier of a positional argument to : so use

 >>> '{0:08b}'.format(1) '00000001' 
+76
Jun 04 '13 at 19:50
source share

I use

 bin(1)[2:].zfill(8) 

will print

 '00000001' 
+10
Dec 10 '15 at 22:58
source share

You can use the string formatting mini-language:

 def binary(num, pre='0b', length=8, spacer=0): return '{0}{{:{1}>{2}}}'.format(pre, spacer, length).format(bin(num)[2:]) 

Demo:

 print binary(1) 

Output:

 '0b00000001' 

EDIT: based on the idea of ​​@Martijn Pieters

 def binary(num, length=8): return format(num, '#0{}b'.format(length + 2)) 
+6
Jun 04 '13 at 19:49
source share

Sometimes you just need a simple one liner:

 binary = ''.join(['{0:08b}'.format(ord(x)) for x in input]) 

Python 3

+1
Jan 01 '16 at 14:02
source share

You can use something like this

 ("{:0%db}"%length).format(num) 
0
Mar 31 '15 at 16:13
source share
 module Adder( input upperBit, lowerBit, c_in, output s, c_out) write gate1, gate2, gate3 xor (gate1, upperBit, lowerBit) xor (s, gate1, c_in) and (upperBit, lowerBit) and (gate1, c_in) or (c_out, gate1, gate2) endmodule module ful_adder8( input [7:0) a, b input c_in output [7:0) s, output c_out) write [7:0] carry full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) full_adder fa0( a(a[o]) b(b[0]) c_in(c_in) s(s[0]) c_out(carry[0])) endmodule test def split (n): return (n&0x1,n&0x2,n&0x4,n&0x8,n&0x10,n&0x20,n&0x40,n&0x80) def glue (b0,b1,b2,b3,b4,b5,b6,b7,c): t = 0 if b0: t += 1 if b1: t += 2 if b2: t += 4 if b3: t += 8 if b4: t += 16 if b5: t += 32 if b6: t += 64 if b7: t += 128 if c: t += 256 return t def myadd (a,b): (a0,a1,a2,a3,a4,a5,a6,a7) = split(a) (b0,b1,b2,b3,b4,b5,b6,b7) = split(b) (s0,s1,s2,s3,s4,s5,s6,s7,c) = addEightBits(a0,a1,a2,a3,a4,a5,a6,a7,b0,b1,b2,b3,b4,b5,b6,b7,false) return glue (s0,s1,s2,s3,s4,s5,s6,s7,c) 
0
Dec 27 '15 at 10:59
source share

You can use zfill:

 print str(1).zfill(2) print str(10).zfill(2) print str(100).zfill(2) 

prints:

 01 10 100 

I like this solution, because it helps not only when outputting a number, but when you need to assign it to a variable ... for example - x = str (datetime.date.today (). Month) .zfill (2) will return x as' 02 'for a month feb.

0
Sep 20 '16 at 14:27
source share



All Articles