Convert an integer to base36

2.4k Views Asked by At

strtoi(x,base=36) will convert a base36-encoded string to an integer:

strtoi("zzzz",base=36)
[1] 1679615

Is there a function that inverts this operation, i.e., given a positive integer yields the base36 equivalent? Essentially, I'm looking for an itostr() function such that

itostr(1679615,base=36)
[1] "zzzz"

(I don't need any base other than 36, but a base argument would be nice to have.)

3

There are 3 best solutions below

0
justin1.618 On BEST ANSWER

I believe if you install the package BBmisc, it has the itostr function is available.

library(BBmisc)
itostr(1679615,base=36)
[1] "zzzz"
1
Joshua Ulrich On

I don't know of any implementations, but the algorithm isn't that difficult. Here's one that works on 32-bit signed integers.

intToBase36 <- function(int) {
  stopifnot(is.integer(int) || int < 0)

  base36 <- c(as.character(0:9),LETTERS)
  result <- character(6)
  i <- 1L
  while (int > 0) {
    result[i] <- base36[int %% 36L + 1L]
    i <- i + 1L
    int <- int %/% 36L
  }
  return(paste(result, sep="", collapse=""))
}

You could use the bit64 and Rmpfr packages if you need to support larger integers.

2
hrbrmstr On

A quick Rcpp hack of this will get you it as well:

library(inline)

cxxfunction(signature(x="numeric"), body='
unsigned int val = as<unsigned int>(x);
static char const base36[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string result;
result.reserve(14);
do {
  result = base36[val % 36] + result;
} while (val /= 36);
return wrap(result);
', plugin="Rcpp") -> base36enc

base36enc(36)
## [1] "10"

base36enc(72)
## [1] "20"

base36enc(73)
## [1] "21"

It definitely needs a bit more code for production use, though.

The BBmisc package referenced in another answer is also C-backed so it's probably a good, performant choice.