Re: print format for binary representation
From: Jp Calderone (exarkun_at_intarweb.us)
Date: Tue, 27 Jan 2004 10:10:10 -0500 To: firstname.lastname@example.org
On Tue, Jan 27, 2004 at 07:00:23AM -0800, Rim wrote:
> >>> print '%x' % 54
> >>> print '%b' % 54
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> ValueError: unsupported format character 'b' (0x62) at index 1
> No formating string for binary? There %x for hex. Would %b make sense
> for bin? Is this worth a PEP?
> How do I get 00110110 printed instead of the traceback?
Oft requested, oft rejected. Defining binary() is about one line of code.
Hex is supported because printf(3) supports it.
Here's a version to toy with:
binary = lambda i, c = (lambda i, c: i and (c(i >> 1, c) + str(i & 1)) or ''): c(i, c)