473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

print format for binary representation

Rim
Hi,
print '%x' % 54 36 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?

Thanks,
-Rim
Jul 18 '05 #1
3 4352
On Tue, Jan 27, 2004 at 07:00:23AM -0800, Rim wrote:
Hi,
print '%x' % 54 36 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)

Jp
Jul 18 '05 #2
> 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)


Is this example in perl or python?

My brain hurts!

:-)


Jul 18 '05 #3
ri*******@yahoo.com (Rim) wrote in message news:<6f**************************@posting.google. com>...
Hi,
print '%x' % 54 36 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?


Yes, it would. No, it isn't.

What we really need is an inverse to the int constructor. Something like:

def itoa(x, base=10):
isNegative = x < 0
if isNegative:
x = -x
digits = []
while x > 0:
x, lastDigit = divmod(x, base)
digits.append('0123456789abcdefghijklmnopqrstuvwxy z'[lastDigit])
if isNegative:
digits.append('-')
digits.reverse()
return ''.join(digits)
How do I get 00110110 printed instead of the traceback?


print itoa(54, 2).zfill(8)
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark Dufour | last post by:
Hi all, I need to convert an integer into some binary representation. I found the following code in the online cookbook (adapted to return a list): binary = lambda n: n>0 and +binary(n>>1) or ...
10
by: 63q2o4i02 | last post by:
Hi, I'm using python to run some lab equipment using PyVisa. When I read a list of values from the equipment, one of the fields is 32 bits of flags, but the value is returned as a floating...
3
by: shyha | last post by:
Hello! Does anybody know what is binary representation of integer datatype fields written to archlogs on z/OS (OS/390) machines? Is it "Two's complement", "One's complement", Sign-modulo or...
3
by: PebblePicker | last post by:
hi all, is it possible to change print format to landscape using javascript/css or anything else which is suppoted in MOZILLA ? I could not find it anywhere.
5
by: Andrea | last post by:
Hi, I'm a newbie i want to know how can i convert string to its binary representation, i want to write a program where an example string "hello world" is transformed in his binary representation...
26
by: Carramba | last post by:
Hi! How can I output value of char or int in binary form with printf(); ? thanx in advance
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
42
by: davidComSee | last post by:
Hi all: char c = 'a'; To get the hex version of the byte c, i can simply use: printf( " %x ",c); But i couldn't find a control letter for binary output.
7
by: eliben | last post by:
Hello, I'm interested in converting integers to a binary representation, string. I.e. a desired function would produce: dec2bin(13) ="1101" The other way is easily done in Python with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.