convert integer to a string in a given numeric base in python

Posted by Mark Borgerding on Stack Overflow See other posts from Stack Overflow or by Mark Borgerding
Published on 2010-02-15T16:33:01Z Indexed on 2010/04/02 22:33 UTC
Read the original article Hit count: 479

Filed under:
|
|
|

Python allows easy creation of an integer from a string of a given base via

int(str,base). 

I want to perform the inverse: creation of a string from an integer. i.e. I want some function int2base(num,base)
such that:

int( int2base( X , BASE ) , BASE ) == X 

the function name/argument order is unimportant

For any number X and base BASE that int() will accept.

This is an easy function to write -- in fact easier than describing it in this question -- however, I feel like I must be missing something.

I know about the functions bin,oct,hex; but I cannot use them for a few reasons:

  • Those functions are not available on older versions of python with which I need compatibility (2.2)
  • I want a general solution that can be called the same way for different bases
  • I want to allow bases other than 2,8,16

Related

© Stack Overflow or respective owner

Related posts about python

Related posts about base