Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: integer to binary 0-padded

Reply
Thread Tools

Re: integer to binary 0-padded

 
 
Olivier LEMAIRE
Guest
Posts: n/a
 
      06-15-2011
You're right, I use Python 2.6.6
 
Reply With Quote
 
 
 
 
John S
Guest
Posts: n/a
 
      06-17-2011
On Jun 15, 9:33*am, Olivier LEMAIRE <m.olivier.lema...@gmail.com>
wrote:
> You're right, I use Python 2.6.6


This works great in 2.6.5 and later (and probably earlier). You just
have to number your placeholders. The first set of braces formats i
(your value), the second set specifies the field with (i.e., :

>>> for i in xrange(10):

.... print "{0:0{1}b}".format(i,
....
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
 
Reply With Quote
 
 
 
 
jmfauth
Guest
Posts: n/a
 
      06-18-2011
>>> '{:+#0{}b}'.format(255, 1 + 2 + 16)
+0b0000000011111111
>>> '{:+#0{}b}'.format(-255, 1 + 2 + 16)

-0b0000000011111111
>>>
>>> eval('{:+#0{}b}'.format(255, 1 + 2 + 16))

255
>>> eval('{:+#0{}b}'.format(-255, 1 + 2 + 16))

-255
>>>


jmf
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-18-2011
On Fri, 17 Jun 2011 23:14:09 -0700, jmfauth wrote:

>>>> '{:+#0{}b}'.format(255, 1 + 2 + 16)

> +0b0000000011111111
>>>> '{:+#0{}b}'.format(-255, 1 + 2 + 16)

> -0b0000000011111111
>>>>
>>>> eval('{:+#0{}b}'.format(255, 1 + 2 + 16))

> 255
>>>> eval('{:+#0{}b}'.format(-255, 1 + 2 + 16))

> -255



Is this a question? Or did you just decide to share some code?

Please leave enough context so that readers can understand what you are
talking about.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57