hash - D语言无符号字符串散列

标签 hash d

我是 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/

相关文章:

multithreading - D编程语言中控制并行线程的数量

c# - 重现 SqlMembershipProvider 密码哈希

c++ - 为什么我的 D 代码查找素数比我的 C++ 代码快得多?

d - 在脚本意义上使用 dlang

Jquery:如何自动将 window.location.hash 应用为与按钮 id 中相同的数字?

d - 从 D 中的数组中删除所有出现的给定值

d2 : difference between allocating array through new and assigning array literal

arrays - Ruby——将嵌套散列转换为多维数组

hash - 玩!哈希密码返回错误结果

hash - 在 Swift 中编写一个好的 Hashable 实现