9

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 returned a hash code in a character type. Is there any way I can turn it into a integer? Or is there any other package to achieve this?

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Xin
  • 575
  • 4
  • 20

3 Answers3

11

That output is a hex (base 16) string. Use following function to change it to decimal. Taken from another forum post but link does not work anymore (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 better answer is strtoi: as @Andrie said and @Gedrox answered, base::strtoi function works in the same way.

strtoi("4da5b0f8", 16)
[1] 1302704376
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
  • 1
    Very nice, but note that R has a built-in function to do this, called `strtoi`, as suggested by another answer. – Andrie May 24 '17 at 13:37
  • 1
    Nice, but how do you avoid integer overflow? – Simon Woodward Mar 14 '19 at 20:09
  • 1
    `stringr::str_sub(a, end=-2)` removes the last character of `a` to ensure it fits within a 32 bit integer. – Simon Woodward Mar 14 '19 at 22:48
  • 1
    But how to strtoi() this hash (sha256 result): "494414ded24da13c451b13b424928821351c78fce49f93d9e1b55f102790c206"? – cineS. Sep 03 '21 at 06:17
  • This number is bigger than .Machine$integer.max. strtoi will not work on it. hex_to_int will also give you approximate double. You need to write custom code to get exact string representation. – Atilla Ozgur Sep 03 '21 at 08:50
9

Since version 0.6.19, digest has a digest2int function, though there is no choice of algorithm. The algorithm used is Jenkin's one_at_a_time.

digest::digest2int("key_a")
#> [1] 1414969953
Aurèle
  • 12,545
  • 1
  • 31
  • 49
4

There is a built in function base::strtoi:

> strtoi("4da5b0f8", 16)
[1] 1302704376
Gedrox
  • 3,592
  • 1
  • 21
  • 29