[Python-bugs-list] [ python-Bugs-465045 ] base n integer to string conversion

noreply@sourceforge.net noreply@sourceforge.net
Wed, 26 Sep 2001 09:00:34 -0700


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:

1) 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.

2) 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.

3) 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: Tim Peters (tim_one)
Date: 2001-09-26 09:00

Message:
Logged In: YES 
user_id=31435

1) 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.

2) I expect Paul means unbounded ints (based on previous 
requests).

3) 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

1) Which bases besides 8, 10 and 16 do you plan to use? The
C code strikes me as more general than needed.

2) You can do this with the struct module. No need for
kludges.

3) 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