# Integer to string with given radix

Write the shortest function to convert an integer into a numeric representation given a radix between 2 and 62. e.g.:

to_string(351837066319, 31) => "codegolf"

• From the example I gather that lower case letters come first, i.e. the digits in base 62 are 0-9,a-z,A-Z in that order? Feb 5 '11 at 0:07
• Yes, exactly, that's it. Feb 5 '11 at 18:20
• at 0:00, dang, perfect. Aug 27 '17 at 21:02

## dc - 43 chars

[sb[58-]s_[lb~dZ39*+dB3<_9+rd0<xrP]dsxxk]sf


We can shorten this a little if we assume the stack contains only the two arguments.

[[58-]s_dSb~dZ39*+dB3<_9+SadLbr0<fLaPc]sf


As a standalone program, we only need 37 characters:

?o[58-]s_[O~dZ39*+dB3<_9+rd0<xrP]dsxx


Instead of using [39+]sz9<z, we simply use Z39*+, which will add 39 for a single digit number, and 78 for a double digit number. Instead of 113, we use B3 (AD also works).

# Ruby 1.8 - 75 characters, with recursion.

f=proc{|n,b|(n<b ? "":f[n/b,b])+([*'0'..'9']+[*'a'..'z']+[*'A'..'Z'])[n%b]}


### Without recursion

f=proc{|n,b|d=[*'0'..'9']+[*'a'..'z']+[*'A'..'Z'];s=d[n%b];s=d[n%b]+s while(n/=b)>0;s}


(both based on Dogbert's 1.9 solution.)

# Python - 86

from string import*
def t(n,r,s=''):
while n:s=printable[n%r]+s;n/=r
return s or'0'


Credit due to Hoa Long Tam for the string import trick

# Python, 93 99

from string import *
d=digits+letters
def t(n,b):
s=''
while n>0:s=d[n%b]+s;n/=b
return s or '0'


EDIT: " or '0'" added for empty string case

• Fails when n = 0, returns an empty string when it should return '0'. Anyway +1 for the string trick
– Juan
Feb 5 '11 at 3:52

# dc, 61 chars

[sr[lr~rd0<x]dsxxk[39+]sa[58-]sb[d9<ad78<b48+anz0<p]dspxIP]sf


Run as:

dc -e'[sr[lr~rd0<x]dsxxk[39+]sa[58-]sb[d9<ad78<b48+anz0<p]dspxIP]sf' -e'351837066319 31 lfx'


or:

dc -f base.dc -e '351837066319 31 lfx'


Explanation: We take the number and base on the stack. sr saves the base in register r. The recursive function [lr~rd0<x]dsxx decomposes a number TOS into its digits in base register r. The first digit is always 0, removed from the stack by k (set precision, which by default is 0 also, so it's equivalent to a nop). Then, the recursive function [48+d57<ad122<banz0<p]dspx outputs each digit in ASCII, with the help of functions a ([39+]sa) and b ([58-]sb). IP outputs a newline. The function is stored in register f, and can be invoked by lfx.

• If you move 48+ to the end it saves two (57 and 122 both drop a character). Also a minor oversight is that as a function, you can't assume there is nothing else on the stack, but the problem would be removed if you merged the loops (which would also save a few characters).
– Nabb
Feb 7 '11 at 22:17

# Ruby - 7270 59 chars

f=->n,b{(n<b ? "":f[n/b,b])+[*?0..?9,*?a..?z,*?A..?Z][n%b]}


## Without recursion, 70 chars

f=->n,b{d=*?0..?9,*?a..?z,*?A..?Z;s=d[n%b];s=d[n%b]+s while(n/=b)>0;s}


Test

irb(main):080:0> f[351837066319, 31]
=> "codegolf"
irb(main):081:0> f[0, 31]
=> "0"


# Python 2, 66 bytes

from string import*
f=lambda n,b:n and f(n/b,b)+printable[n%b]or''


Try it online!

# Burlesque, 4 bytes

peB!


Try it online!

Luckily a built-in in Burlesque

pe # Parse and push
B! # Convert base


m=divMod
d(0,x)b=[f x]
d(r,x)b=f x:d(m r b)b
f=(!!)$['0'..'9']++['a'..'z']++['A'..'Z'] s x b=reverse$d(m x b)b

• Feels a bit wrong suggesting golfs to something over 10 years old, but I may as well note that this can be shaved down a good 5 bytes, by factoring the whole d(m r b)b pattern into m (with a bit of help from the Applicative instance for functions), then using that to convert s to pointfree. Jul 5 '21 at 15:25

## Befunge - 53 x 2 = 106 characters

Or 53 + 46 = 99 characters if you're willing to route other parts of your program through the bottom left.

11p01-\>:11g%\11g/:#v_$>:1+!#v_:45+!#v_:75*!#v_ v ^ < ^,$# +"0"  < +"'"   <-":"<


First place the number to be converted on the stack, then the radix and enter this function from the top-left going right. Will output the string for you (since Befunge doesn't support string variables) and leave from the bottom $ going down. Requires the (1,1) cell for radix storage. E.g. for the example given put 351837066319 into the input and run: &56*1+ 11p01-\>:11g%\11g/:#v_$>:1+!#v_:45+!#v_:75*!#v_   v
^            <  ^,    $# +"0" < +"'" <-":"< @  ## Golfscript - 32 chars {base{.9>39*+.74>58*48--}%''+}:f  # Ruby 1.9 - 8074 68 t=->n,b{d=?0..?9,?a..?z,*?A..?Z;s='';(s=d[n%b]+s;n/=b)while n>0;s} With '0' for empty string, 95 89 82 characters: t=->n,b,s=''{d=*?0..?9,*?a..?z,*?A..?Z;(s=d[n%b]+s;n/=b)while n>0;s.empty?? ?0: s}  Ruby 1.9 - unfortunately only works up to base 36: t=->n,b{n.to_s(b)}  • You can replace the ]+['s with ,. Feb 7 '11 at 0:14 # Bash, 79 chars f(){ dc<<<$2o$1p|perl -pe"y/A-Z/a-z/;s/ \d+/chr$&+($&<10?48:$&<36?87:29)/ge"
}

• BC_BASE_MAX is documented as being 16. I don't know what miracle makes the output right on the sample input, but it outputs garbage (i.e. non alphanum characters) for most other bases.
– J B
Feb 6 '11 at 14:30
• @J B: which bc are you using? GNU bc should work. sysconf(_SC_BC_BASE_MAX) returns 99 on my system, 16 is the minimum required. Feb 6 '11 at 14:37
• @J B: also note that previous revisions where buggy, I had just skimmed at the question requirements. Feb 6 '11 at 14:39
• bc 1.06. Now you mention it, I got the figure from the manpage, but misread it. 16 is the input base limit. The output base limit is 999. I did first try on an earlier version, let's see that again now.
– J B
Feb 6 '11 at 14:53
• I think this one outputs uppercase letters for bases 11-16 instead of lower case. You can save a few at the base conversion by using dc instead of bc.
– Nabb
Feb 7 '11 at 8:58

# Jelly, 5 bytes

b‘ịØB


Try it online!

The case will be reversed (codegolf - CODEGOLF) because of Jelly built-in base characters string.

### How?

b‘ịØB - Link, input x & y
b     - Convert x to base y
‘    - Increment (because Jelly use based-1 indexing)
ị   - List (string) indexing
ØB - "0-9 A-Z a-z"


# Japt, 9 bytes

Follows the example of the Jelly solution in reversing the casing.

;sVîEowi%


Try it

# Stax, 5 bytes

VL(:B


Run and debug it

Same size packed, a custom base conversion.

# Perl 5-Minteger -ap, 52 bytes

do{$\=(0..9,a..z,A..Z)[$_%$F[1]].$\}while$_/=$F[1]}{


Try it online!

# Go, 76 bytes

package m
import."math/big"
func e(n int64)string{return NewInt(n).Text(62)}


bonus decode:

package m
import."math/big"
func d(s string)int64{o,_:=NewInt(0).SetString(s,62)
return o.Int64()}