我是 D language 的初学者.
如何以 D 语言中的 uint
无符号 32 位整数的形式获取字符串的一些散列...
我需要一个快速而肮脏的哈希码(我不太关心“随机性”或“缺乏碰撞”,我更关心性能)。
import std.digest.crc;
uint string_hash(string s) {
return crc320f(s);
}
不好...
(在带有 phobos-2 的 Linux/x86-64 上使用 gdc-5
)
最佳答案
虽然 Adams answer 完全符合您的要求,但您也可以使用 union 进行转换。 这是一个非常有用的技巧,所以不妨把它放在这里:
/**
* Returns a crc32Of hash of a string
* Uses a union to store the ubyte[]
* And then simply reads that memory as a uint
*/
uint string_hash(string s){
import std.digest.crc;
union hashUnion{
ubyte[4] hashArray;
uint hashNumber;
}
hashUnion x;
x.hashArray = crc32Of(s); // stores the result of crc32Of into the array.
return x.hashNumber; // reads the exact same memory as the hashArray
// but reads it as a uint.
}
关于hash - D语言无符号字符串散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594880/