前一段时间在网上找以图搜图的资料,发现了感知哈希算法,得出的哈希值以字符串形式保存,我们管这种形式叫做哈希指纹。字符串中为十六进制数,需计算其二进制中的明氏距离,即二进制表示中不一样位数的个数。如下测试代码:
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int LSH(string str1,string str2);
int sub(char a,char b);
int _tmain(int argc, _TCHAR* argv[])
{
string str1 = "9e0f53";
string str2 = "0b672a";
cout<<"str1 = "<<str1.c_str()<<endl;
cout<<"str2 = "<<str2.c_str()<<endl;
int dis = LSH(str1,str2);
cout<<"dis = "<<dis<<endl;
int a;
cin >> a;
return 0;
}
int LSH(string str1,string str2)
{
//int L = sizeof(str1.c_str());
int L = str1.length();
cout<<"L = "<<L<<endl;
int dis = 0;
for(int i=0;i<L;i++)
{
dis += sub(str1[i],str2[i]);
}
return dis;
}
unsigned char char2hex(char a)
{
unsigned char t(0);
switch(a)
{
case 'a': t = 10; break;
case 'b': t = 11; break;
case 'c': t = 12; break;
case 'd': t = 13; break;
case 'e': t = 14; break;
case 'f': t = 15; break;
default: t = a - '0'; break;;
}
return t;
}
int sub(char a,char b)
{
unsigned char a1 = char2hex(a);
unsigned char b1 = char2hex(b);
unsigned char c1 = a1^b1;
cout<<"a1 = "<< (int)a1 <<endl;
cout<<"b1 = "<< (int)b1 <<endl;
//cout<<"c1 = "<< (int)c1 <<endl;
int t(0);
unsigned char tem(1);
for(int i=0;i<8;i++)
{
if(c1&(tem))
t++;
tem = tem<<1;
}
cout<<"t = "<<(int)t <<endl;
return t;
}
运行结果:
<img src="https://img-blog.csdn.net/20140926140419752?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGF1dF96amI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif;" alt="" />