[Python-bugs-list] [ python-Bugs-465045 ] base n integer to string conversion (original) (raw)
noreply@sourceforge.net noreply@sourceforge.net
Fri, 05 Oct 2001 19:09:50 -0700
- Previous message: [Python-bugs-list] [ python-Bugs-467381 ] print statement and exception
- Next message: [Python-bugs-list] [ python-Bugs-468443 ] Python download/install process
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Bugs item #465045, was opened at 2001-09-25 18:42 You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=465045&group_id=5470
Category: Python Library Group: Feature Request Status: Open Resolution: None Priority: 5 Submitted By: paul rubin (phr) Assigned to: Nobody/Anonymous (nobody) Summary: base n integer to string conversion
Initial Comment: I see that the C files for int and long arithmetic already contain format() functions to format the numbers in arbitrary bases. The Python-exported decimal and hex conversion routines simply call the format function.
I have three suggestions/requests, in increasing order of controversy:
How about exporting the format routine to Python, so there's a builtin function for converting an integer object to an arbitrary base 2<=b<=36. The code is already there--may as well let people use it. Something like format(n,base) would be a simple interface.
Right now there's no simple way to convert an integer to a raw byte string. I end up doing something like a = binascii.unhexlify("%x"%n) which is a kludge. I'd like to propose that as a special case, format(n,256) give the raw bytes.
Finally, if it's not too weird, format(n,128) could possibly return the BER (base 128) encoded representation of n, similar to pack("w",n) in perl. Bit 8 (the high bit) is set in each byte except the last. That means you can read BER integers byte by byte and know where they end. They're used in various security objects like X509 certificates. If not here, then sooner or later this function should appear in some library module.
Thanks Paul
Comment By: Guido van Rossum (gvanrossum) Date: 2001-10-05 19:09
Message: Logged In: YES user_id=6380
Various forms of fixed-length int to binary conversion are provided by the struct module.
What's a "surprising deficiency" for you could simply be everybody else's lack of interest -- if nobody needs it, it won't be added to the language... :-)
Comment By: paul rubin (phr) Date: 2001-10-05 16:05
Message: Logged In: YES user_id=72053
Re which bases are useful: I want base 256 all the time and find the lack of any clean way to convert ints to binary is a surprising deficiency in Python. I'm not too concerned about what the interface should be, but I hope one is added.
I agree, BER is a bit obscure, but if you feel it's ok to put it in the binascii module, I might submit a patch. How about if I put base-256 there too?
Comment By: Tim Peters (tim_one) Date: 2001-09-26 09:00
Message: Logged In: YES user_id=31435
Base 2 is semi-frequently requested. Note that Greg Wilson has open patch 455076 to add %b format and bin() function. No non-binary base has any fans.
I expect Paul means unbounded ints (based on previous requests).
Yup. Jeremy's Pisces may already deal w/ this (found via Google search).
3') Pickle already represents ints < 256 in one byte, and less than 2**16 in 2 (plus a 1-byte type code, of course).
Comment By: Guido van Rossum (gvanrossum) Date: 2001-09-26 06:38
Message: Logged In: YES user_id=6380
Which bases besides 8, 10 and 16 do you plan to use? The C code strikes me as more general than needed.
You can do this with the struct module. No need for kludges.
BER seems pretty esoteric. The binascii module seems the right place. Feel free to submit a patch!
Comment By: paul rubin (phr) Date: 2001-09-25 18:45
Message: Logged In: YES user_id=72053
One minor comment: besides representing longs, BER encoded integers are also good for small integers. Numbers < 128 are represented in one byte. If short strings were marshalled with a BER-encoded length instead of a 4-byte length, that could save some space in .pyc files and pickled objects.
You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=105470&aid=465045&group_id=5470
- Previous message: [Python-bugs-list] [ python-Bugs-467381 ] print statement and exception
- Next message: [Python-bugs-list] [ python-Bugs-468443 ] Python download/install process
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]