# Print integers in any base up to 36 [duplicate]

Possible Duplicate:
Integer to string with given radix

This question is inspired by Pythons builtin int function. int is used to convert other numbers or strings into integers. If you are converting a string you can provide the additional parameter base, that contains the base that your number is represented in within the string:

>>> int('137')
137
>>> int('137',base=8)
95
>>> int('ff',base=16)
255
>>> int('1AZ',base=36)
1691


Unfortunately Python doesn't provide a function for the reverse case, where you want to convert a number into a string in a specific base.

• the function may return the value or print it
• you must provide support for all bases between 2 and 36
• digits to use are 0123456789abcdefghijklmnopqrstuvwxyz
• all languages are allowed
• built in functions for this purpose are boring and thus forbidden
• shortest code wins
• "built in functions for this purpose are boring" - and the problem itself isn't? – Peter Taylor Oct 4 '12 at 12:39
• huh. i didnt think it was such a bad question :( – quasimodo Oct 4 '12 at 14:22
• I thought it's a bit boring but it's a good exercise for golf beginners... +1 to even out that unfair -1! – milo5b Oct 4 '12 at 14:51
• In fact, it's a trivial variant on some existing questions, most notably codegolf.stackexchange.com/questions/536/…, – Peter Taylor Oct 4 '12 at 15:08

## APL (25)

{(⎕D,⎕A)[1+⍵⊤⍨⍺⍴⍨⌈⍺⍟⍵+1]}


The left argument is the base, and the right argument is the number.

i.e.

    16{(⎕D,⎕A)[1+⍵⊤⍨⍺⍴⍨⌈⍺⍟⍵+1]}1234
4D2


## C, 68

c(n,b,r){char*p=1+&r;for(r=0;n;n/=b)*--p=n%b+48+(n%b>9)*39;puts(p);}


testing:

main(){
c(65,2);
c(100,30);
c(57005, 16);
}


output:

1000001
3a


issues:

• 0 is printed as empty string.
• potential buffer overflows due to abuse of 'int' as a char array...
• works with Apple clang v4.0 - your milage may vary with other compilers.
• Just to tease you a bit, i've got a working c function in 48 characters, i will release it after i choose a winner :) – quasimodo Oct 5 '12 at 18:12
• Also, it's architecture specific (depends on endianness of system). – Wug Oct 5 '12 at 20:43
• Furthermore, puts appends a newline, so your outputs are vertical. – Wug Oct 5 '12 at 21:15
• The 48 char version is out now, thanks to Wug, but its far less interesting/dangerous than yours. The code that gcc produces is remarkably safe, since r is the last value on the stack and the stack isnt used after that, all you are overwriting is uninitialized stack space. you could even get rid of the 1+, so you would ensure there is always a 0 byte at the end of the string. @Wug: he doesn't print character-wise, so the output is fine. – quasimodo Oct 6 '12 at 10:20

## Befunge, 17x4 = 68 characters

1-&00p&>:!#v_:00v
v-10<  ^/g00\%g <
< >:^@_.# #$v#:! "_^#9::$  <<,+"W


Online interpreter: http://www.bedroomlan.org/tools/befunge-93-playground

First input is base, second is number.

Similar to other solutions here this does not output anything when the number is 0. I may look at fixing this at some point.

## PHP, 92 Bytes

Quite new to code-golf, so probably can do better than this (suggestions would be very welcome!)

function b($n,$b){while($n>0){$r=$n%$b;$n=floor($n/$b);$s=($r>9?chr(87+$r):$r).$s;}echo$s;}  With (some) whitespaces: function b($n,$b) { while ($n>0)
{
$r=$n%$b;$n=floor($n/$b);
$s=($r>9?chr(87+$r):$r).$s; } echo$s;
}

• Next time somebody has to -1 my answer, could you please explain why? I am trying to learn here :) No hard feelings! – milo5b Oct 5 '12 at 8:16
• You could probably shorten it by making the function recursive. Remember, you don't need to print the string, returning it is enough. – quasimodo Oct 6 '12 at 10:35

## Python, 81 58 bytes

x=lambda n,b:(n/b and x(n/b,b)or'')+chr(48+n%b+39*(n%b>9))


Usage: x(100,30) returns '3a', which is 100 in base 30.

• some output characters are not from the required alphabet: x(30,36)='~' – quasimodo Oct 4 '12 at 13:49
• 3a is 100 in base 30 (3*30+10) – SeanC Oct 4 '12 at 16:11
• quasimido and @SeanCheshire - both right, a stupid mistake by me (same bug caused both symptoms). Fixed. – ugoren Oct 4 '12 at 19:52

# C, 48 chars

i(n,b){n?i(n/b,b),putchar(n%b+48+(n%b>9)*39):0;}

• input of 0 produces empty string

# C, 77 49 chars, prints 0 for zero

I(n,b){putchar(n?i(n/b,b),48+n%b+(n%b>9)*39:48);}


To run, call i for first version, I for second.

• @quazimodo: I got this by hax0ring your computer. It was hard because you had norton, but you'll never catch me because im behind 7 proxies. – Wug Oct 5 '12 at 21:25
• youre so 1337! But i need just one more char to print zero as 0 :D – quasimodo Oct 6 '12 at 9:29
• @quasimodo: like this? :D – Wug Oct 8 '12 at 7:55
• more like this: c(n,b){n/b&&c(n/b,b);putchar((n%b>9)*39+48+n%b);}. But yours is also nice! – quasimodo Oct 8 '12 at 12:10

## C# – 85 characters

string F(int n,int b){var s="";for(;n>0;n/=b)s=(char)(n%b+(n%b>9?87:48))+s;return s;}


## VBA, 99

(70 if you take out the function/endfunction lines)

Function F(a,b)
F="":q=a:Do:c=q Mod b:F=Chr(c+48-39*(c>9))&F:q=Int(q/b):Loop Until q=0
End Function


fixed for case when a=0

# Ocaml, 98 or 120

It prints out a leading zero, though. Usage: 16-0xBEEF

let rec(-)b=function|0->"0"|n->b-n/b^String.make 1(Char.chr(n mod b+if n mod b>9 then 55 else 48))


Unfortunately a lot of characters are spent on converting to char then to string :/

This one is correct.

let rec(-)b=function|0->"0"|n->(if 0=n/b then""else b-n/b)^String.make 1(Char.chr(n mod b+if n mod b>9 then 55 else 48))


The strong typing in ocaml is sometimes unfortunate when golfing. Partial application and such somewhat make up for it, though.

## CoffeeScript (98 92)

z=(i,b)->a=parseInt(i)%b;return''if 1>i;(z i/b,b)+if a>9then String.fromCharCode(a+87)else a


The compiled (and readable) javascript for the interested:

z = function(i, b) {
var a;
a = parseInt(i) % b;
if (1 > i) {
return '';
}
return (z(i / b, b)) + (a > 9 ? String.fromCharCode(a + 87) : a);
};


What can I say? Unnecessary recursion gets me hot.

This could have been far shorter if I knew a shorter way to floor or convert from ASCII in Coffee-/Javascript. Tips would be greatly appreciated.

Edited to include "parseInt" to save a couple of characters on "Math.floor" and with some rearranging to reduce the "if ... then ..."s. Unfortunately, I don't think there even is a shorthand if in CoffeeScript ( "... ? ... : ..." doesn't work).

• parseInt() would be 2 char shorter of Math.floor, and I believe would have the same effect – milo5b Oct 5 '12 at 15:58
• you don't need Math.floor if you are using String.fromCharCode to print the numbers, also you can probably shorten the code a bit by using shortcut operators instead of ifs. – quasimodo Oct 5 '12 at 18:39