# changeBase -- change integer base

## Synopsis

• Usage:
changeBase(n, newbase)
changeBase(str, oldbase)
changeBase(str, oldbase, newbase)
• Inputs:
• Outputs:

## Description

When both arguments are integers, the return value is a string representation of the first argument in the base given by the second argument.

 i1 : changeBase(255, 16) o1 = ff

Bases from 2 to 62 are supported. If newbase is between 11 and 36, then the digits representing 10 through newbase - 1 are the lowercase letters.

 i2 : for n from 10 to 35 list changeBase(n, 36) o2 = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, ------------------------------------------------------------------------ y, z} o2 : List

If newbase is between 37 and 62, then the digits representing 10 through 35 are uppercase letters and the digits representing 36 through newbase - 1 are lowercase letters.

 i3 : for n from 10 to 61 list changeBase(n, 62) o3 = {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, ------------------------------------------------------------------------ Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, ------------------------------------------------------------------------ w, x, y, z} o3 : List

If the first argument is a string, then we get the inverse operation, i.e., the ZZ object that the string represents in the given base.

 i4 : changeBase("ff", 16) o4 = 255

Uppercase letters are also allowed when oldbase is between 11 and 36, as there is no ambiguity.

 i5 : changeBase("FF", 16) o5 = 255

If oldbase is 0, then the base is determined by the standard C prefix, i.e., 0b or 0B for binary, 0 for octal, 0x or 0X for hexadecimal, and no prefix for decimal.

 i6 : apply({"0b10", "0B10", "010", "0x10", "0X10", "10"}, s -> changeBase(s, 0)) o6 = {2, 2, 8, 16, 16, 10} o6 : List

If a string and two integer arguments are given, then the integer represented by the string is converted from one base to another, returning a string.

 i7 : changeBase("ff", 16, 2) o7 = 11111111

## Ways to use changeBase :

• "changeBase(String,ZZ)"
• "changeBase(String,ZZ,ZZ)"
• "changeBase(ZZ,ZZ)"

## For the programmer

The object changeBase is .