将无符号整数转换回char *?

时间:2015-05-05 01:47:39

标签: c++ unsigned-integer

我有一个哈希函数,在给定uint32_t的情况下返回给我一个无符号整数char*,如下所示:

uint32_t key_hashing(const char* key)
{
    return hashing(key, 0x7fffffff, 101);
}

uint32_t hashing(const char* word, int tsize, uint32_t seed)
{
    char c;
    uint32_t h = seed;
    for ( ; (c=*word) != '\0'; ++word)
    {
        h ^= ( (h<<5) + c + (h >> 2) );
    }
    return ((uint32_t)(h&0x7fffffff) % tsize);
}

现在我想做相反的事情:让我们说我已经获得了uint32_t个号码,我想恢复到早先在char*中的原始形式。我怎样才能做到这一点?基本上我的uint32_t号码应该给我原来的char *。

1 个答案:

答案 0 :(得分:1)

由于散列冲突,这不会起作用:可能有多个字节数组的排列,但只有固定数量的不同uint32_t,因此一堆不同的char *将被哈希到相同的 uint32_t值。