How do I get the hashcode as an integer in R?

What I want to do is implement a hash trick in R.

Code below:

library(digest)
a<-digest("key_a", algo='xxhash32')
#[1] "4da5b0f8"

      

This returns the hash code in the character type. Is there a way to convert it to an integer? Or is there another package to achieve this?

+5


source to share


2 answers


This output is a hexadecimal (base 16) string. Use the following function to change it to decimal. Taken from another forum post but link no longer works (2017).

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)))
}

      

Output



> hex_to_int(a)
[1] 1302704376

      

But the best answer is strtoi : as @Andrie said and @Gedrox answered: base :: strtoi The a> function works the same.

strtoi("4da5b0f8", 16)
[1] 1302704376

      

+8


source


There is a built-in function base::strtoi

:



> strtoi("4da5b0f8", 16)
[1] 1302704376

      

+4


source







All Articles