This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: base n integer to string conversion
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: gvanrossum, phr, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2001-09-26 01:42 by phr, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (9)
msg6679 - (view) Author: paul rubin (phr) Date: 2001-09-26 01:42
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
msg6680 - (view) Author: paul rubin (phr) Date: 2001-09-26 01:45
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.
msg6681 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-09-26 13:38
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!
msg6682 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-09-26 16:00
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).
msg6683 - (view) Author: paul rubin (phr) Date: 2001-10-05 23:05
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?
msg6684 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-10-06 02:09
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... :-)
msg6685 - (view) Author: paul rubin (phr) Date: 2001-10-12 07:20
Logged In: YES 
user_id=72053

The struct module doesn't give any way of converting
arbitrary ints (meaning longs) to binary.  Really, it's
needed.  Some people do it with gmpy, but if Python is
going to support longs as a built-in type, one shouldn't
have to resort to 3rd-party modules to read and write
them in binary.
msg6686 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-10-12 14:27
Logged In: YES 
user_id=6380

OK, I believe you.  Can you submit a patch?
msg6687 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-06-25 00:39
Logged In: YES 
user_id=80475

This is feature request appears to be suffering from lack of 
interest.  If no one speaks-up for it by 27 July 2002, I'll 
close it.

If it is needed, I would be happy to help implement it.
History
Date User Action Args
2022-04-10 16:04:28adminsetgithub: 35235
2001-09-26 01:42:05phrcreate