Converting a hexadecimal string to an integer in R

In R, if you want to convert a string representing an integer in base 16 to an integer, then you can simply use as.integer as long as you prefix the string with "0x". Here's an example:

as.integer("0x4d2")
[1] 1234

Originally, I had posted the following excerpt from as.hexmode because I found the implementation elegant and a good example of how to make use of R's functional and vectorized features. I didn't know that as.integer knew how to do that. It is documented, of course, but help.search("hexadecimal") does not give a hit there and led me astray. I received a comment on the original post that clued me in.

## from R's as.hexmode, how to convert a hexadecimal string to an int
hex_to_int = function(h) {
  xx = strsplit(tolower(h), "")[[1L]]
  pos = match(xx, c(0L:9L, letters[1L:6L]))
  sum((pos - 1L) * 16^(rev(seq_along(xx) - 1)))
}

For another project, I needed to implement a base 62 conversion function to generate URL-safe strings for a URL shortener. Base 62 is what you can represent easily using [a-zA-Z0-9]. I wrote the function in Erlang and when I came across the above implementation in R, it made me wonder whether there was a cleaner approach perhaps using lists:map instead of recursion like:

decode([C | T], N, Acc) ->
    decode(T, N * 62, char_to_digit(C) * N + Acc);
decode([], _N, Acc) ->
    Acc.

archived on 2009-05-15 in

blog comments powered by Disqus