十六进制哈希比较

时间:2013-11-05 18:38:55

标签: mysql hash comparison hex padding

我将哈希值存储在bit(64)类型的表上。其中一些哈希记录无法通过select直接匹配来检索 的说明:

我使用命令

在数据库中插入每个哈希
insert into hashes (id, hash) values (0, 0xad66f2f8f3815456);

然后,我用

检索记录
select id from hashes where hex(hash) = 'ad66f2f8f3815456';
+------+
| id   |
+------+
|    0 |
+------+

只要散列不是零填充,这就可以工作:

insert into hashes (id, hash) values (1, 0x0d66f2f8f3815456);
select id from hashes where hex(hash) = '0d66f2f8f3815456';
Empty set (0.00 sec)

通过匹配ID来检索记录会导致:

select id, hex(hash) from hashes where id = 1;
+------+-----------------+
| id   | hex(hash)       |
+------+-----------------+
|    1 | D66F2F8F3815456 |
+------+-----------------+

所以我想我无法在存储的哈希和提示的哈希之间进行数字匹配,无论填充零。我正在使用的表是:

describe hashes;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(4)  | YES  |     | NULL    |       |
| hash  | bit(64) | YES  | MUL | NULL    |       |
+-------+---------+------+-----+---------+-------+

这怎么解决?这是处理需要进行数值比较的64位哈希记录的正确方法吗? 我有几千条这样的记录,因此它们不应该被存储为繁琐的varchars。

1 个答案:

答案 0 :(得分:1)

索引变量实际上工作得很好,但如果你总是使用64位值,你也可以使用unsigned bigint来实现相同的结果,并且可以轻松转换为散列的数字表示代码。