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

noreply@sourceforge.net noreply@sourceforge.net
Tue, 25 Sep 2001 18:45:44 -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: 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