String hash value's distance comparison

Source: Internet
Author: User

Some time ago, I searched for graph data on the Internet and found the hash algorithm. The obtained hash value is saved as a string. In this form, it is called a hash fingerprint. The string is a hexadecimal number, which is the number of different digits in binary. The following test code:

// Test. cpp: defines the entry point of the console application. // # 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 ;}
Running result:
 

String hash value's distance comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.