如何在R中将哈希码获取为整数? [英] How to get a hash code as integer in R?
本文介绍了如何在R中将哈希码获取为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的是在R中实现哈希技巧.
What I want to do is implement a hash trick in R.
以下代码:
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?
推荐答案
该输出为十六进制(基数为16)字符串.使用以下功能将其更改为十进制.取自另一篇论坛帖子,但链接不再起作用(2017年).
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)))
}
输出
> hex_to_int(a)
[1] 1302704376
但是更好的答案是 strtoi :正如@Andrie所说和@Gedrox回答的那样,
But better answer is strtoi: as @Andrie said and @Gedrox answered, base::strtoi function works in the same way.
strtoi("4da5b0f8", 16)
[1] 1302704376
这篇关于如何在R中将哈希码获取为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文