Hamming Distance
BITWISE XOR - HAMMING DISTANCE between two long HEX values
<br/>
<br/>
e.g. find similar images or anything else that has a hashvalue
<br/>
<br/>
comparehash - returns similarity in percent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
MYSQL Function
*/
DELIMITER $$
DROP FUNCTION IF EXISTS comparehash $$
CREATE FUNCTION comparehash (hash1 varchar(256), hash2 varchar(256), maxlen int)
RETURNS float
DETERMINISTIC
BEGIN
DECLARE hashpart1 varchar(64) DEFAULT "";
DECLARE hashpart2 varchar(64) DEFAULT "";
DECLARE bitcnt int DEFAULT 0;
DECLARE strlen int DEFAULT 16;
DECLARE i int DEFAULT 0;
DECLARE len int DEFAULT 0;
SET len = LENGTH(hash1) / strlen;
WHILE i<len AND i < maxlen DO
SET hashpart1 = SUBSTRING(hash1,(i*strlen)+1,strlen);
SET hashpart2 = SUBSTRING(hash2,(i*strlen)+1,strlen);
SET bitcnt = bitcnt + bit_count(cast(conv(hashpart1, 16, 10) as unsigned) ^ cast(conv(hashpart2, 16, 10) as unsigned));
SET i = i+1;
END WHILE;
RETURN ((64*i)-bitcnt)*100.0/(64*i);
END $$
DELIMITER ;
X
Url: http://www.medienservice-ladewig.de/MYSQL-Hamming-Distance.2099932101.html
Language: SQL | User: Jörg | Created: Oct 21, 2013 | Tags: mysql hash hex compare hamming distance function