Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding whole numbers in R to a base 62 character vector

Tags:

r

What's a quick way to encode either integer values or numeric whole number values in R to a character vector in base 62 encoding, i.e. a string that only contains [a-zA-Z0-9]? Would translating the answer to this question be sufficient? converting a number base 10 to base 62 (a-zA-Z0-9)

Edited

Here's my solution:

toBase <- function(num, base=62) {
    bv <- c(seq(0,9),letters,LETTERS)
    r <- num %% base
    res <- bv[r+1]
    q <- floor(num/base)
    while (q > 0L) {
        r <- q %% base
        q  <- floor(q/base)
        res <- paste(bv[r+1],res,sep='')
    }
    res
} 
to10 <- function(num, base=62) {
    bv <- c(seq(0,9),letters,LETTERS)
    vb <- list()
    for (i in 1:length(bv)) vb[[bv[i]]] <- i
    num <- strsplit(num,'')[[1]]
    res <- vb[[num[1]]]-1
    if (length(num) > 1)
         for (i in 2:length(num)) res <- base * res + (vb[[num[i]]]-1)
    res
}

Is that missing anything?

like image 908
Jeff Avatar asked Oct 11 '22 10:10

Jeff


1 Answers

Here's a solution that does base 36 using [0-9A-Z] that could easily be adapted for base 62 using [a-zA-Z0-9]. And yes, it's basically just a translation of the solution to the other question you linked to.

https://github.com/graywh/r-gmisc/blob/master/R/baseConvert.R

like image 186
graywh Avatar answered Oct 17 '22 14:10

graywh