1

I have two hex strings:

string x = "928fe46f228555621c7f42f3664530f9";
string y = "56cd8c4852cf24b1182300df2448743a";

I'm trying to convert them to binary to find how many bits matches between the two hex strings.

I used this function to convert HEX to Binary:

 string GetBinaryStringFromHexString (string sHex)
 {
     string sReturn = "";
     for (int i = 0; i < sHex.length (); ++i)
     {
         switch (sHex [i])
         {
             case '0': sReturn.append ("0000"); break;
             case '1': sReturn.append ("0001"); break;
             case '2': sReturn.append ("0010"); break;
             case '3': sReturn.append ("0011"); break;
             case '4': sReturn.append ("0100"); break;
             case '5': sReturn.append ("0101"); break;
             case '6': sReturn.append ("0110"); break;
             case '7': sReturn.append ("0111"); break;
             case '8': sReturn.append ("1000"); break;
             case '9': sReturn.append ("1001"); break;
             case 'a': sReturn.append ("1010"); break;
             case 'b': sReturn.append ("1011"); break;
             case 'c': sReturn.append ("1100"); break;
             case 'd': sReturn.append ("1101"); break;
             case 'e': sReturn.append ("1110"); break;
             case 'f': sReturn.append ("1111"); break;
         }
     }
     return sReturn;
 }

So String x in binary is--> 10010010100011111110010001101111001000101000010101010101011000100001110001111111010000101111001101100110010001010011000011111001

and String y in binary is --> 01010110110011011000110001001000010100101100111100100100101100010001100000100011000000001101111100100100010010000111010000111010

But now I'm stuck, how can I xor the two strings to find the number of matching bits ? and how can I count them?

It doesn't matter whether I use Java or C++, can anyone help please

Thank you,

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Pedro Xyze
  • 11
  • 2

3 Answers3

4

In Java it's pretty easy.

public static int numberOfMatchingOnes(String a, String b) {
    BigInteger aNumber = new BigInteger(a, 16);
    BigInteger bNumber = new BigInteger(b, 16);

    return aNumber.xor(bNumber).bitCount();
}

In C++ you might use a bitset. What you're looking for is called Hamming weight.

If you really want to do it without BigInteger: Take 4 chars of both strings, convert them into an int, xor them and count the one-bits. Repeat until the strings end.

public static int numberOfMatchingOnes(String a, String b) {
    if (a.length() != b.length() || a.length() % 4 != 0) {
        throw new IllegalArgumentException("invalid strings");
    }

    int totalCount = 0;
    for (int i = (a.length()-1)/4; i >= 0; i--) {
        int aValue = Integer.valueOf(a.substring(i * 4, i * 4 + 4), 16);
        int bValue = Integer.valueOf(b.substring(i * 4, i * 4 + 4), 16);
        totalCount += Integer.bitCount(aValue ^ bValue);
    }
    return  totalCount;
}

You can look at the Java Sourcecode to see how bitCount() works.

dusky
  • 1,133
  • 7
  • 12
1

You have two strings. Why not run through them character by character and see if they match or not? Initialize a counter to zero and start incrementing them for each match and display at the end of the loop. Much simpler.

Here is a one-liner solution though (with the power of all the libraries in the world):

System.out.println(StringUtils.countMatches(new BigInteger("928fe46f228555621c7f42f3664530f9",16).xor(new BigInteger("56cd8c4852cf24b1182300df2448743a",16)).toString(2),"1"));
toddlermenot
  • 1,588
  • 2
  • 17
  • 33
0

It depends your purpose if you want to find where they matched together you can use AND in C :

#include <stdio.h>
int toBinary(unsigned int b1,unsigned int b2){
unsigned int b = b1 & b2;
printf("%x & %x = %x\n",b1,b2,b);
}
int main(){
int i,a,b;
unsigned int b1 = 0x100100;
unsigned int b2 = 0x010101;
toBinary(b1,b2);
return 0;
} 

the above code from right to left compare numbers in binary and wherever a two bit were 1 then return 1 else return 0

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if you want to find where are the same or not use XOR in C :

#include <stdio.h>
int toBinary(unsigned int b1,unsigned int b2){
unsigned int b = b1 ^ b2;
printf("%x & %x = %x\n",b1,b2,b);
}
int main(){
int i,a,b;
unsigned int b1 = 0x100100;
unsigned int b2 = 0x010101;
toBinary(b1,b2);
return 0;
} 
SdSaati
  • 798
  • 9
  • 18