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 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
> 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!
:-) 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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 ...
|
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...
|
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...
|
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.
|
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...
|
by: Carramba |
last post by:
Hi!
How can I output value of char or int in binary form with printf(); ?
thanx in advance
|
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...
|
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.
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
| |