Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

Idiom #308 Integer to string in base b

Create the string representation s of the integer value n in base b.

18 in base 3 -> "200"
26 in base 5 -> "101"
121 in base 12 -> "a1"

let s = n.toString(b);
String s = Integer.toString(n, b);
math
function IntToBaseStr(n: longword; const b: cardinal): string;
const
  digits = '0123456789abcdefghijklmnopqrstuvwxyz';
var
  remainder: longword;
begin
  Result := '';
  repeat
    DivMod(n, b, n, remainder);
    result := digits[remainder + 1] + result;
  until n = 0;
end;
def int_to_base_str(n, b):
    digits = '0123456789abcdefghijklmnopqrstuvwxyz'
    s = ''
    if n == 0: return '0'
    while n:
        n, remainder = divmod(n, b)
        s = digits[remainder] + s
    return s
s = n.to_s(b)

New implementation...
< >
lesmana