比较两个十六进制字符串以查找匹配位数 [英] Compare two hex strings to find number of matching bits

查看:223
本文介绍了比较两个十六进制字符串以查找匹配位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个十六进制字符串:

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

我试图将它们转换为二进制来查找两个十六进制字符串之间有多少位匹配。



我使用这个函数将HEX转换为Binary:

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





$ b因此二进制字符串x是 - >
10010010100011111110010001101111001000101000010101010101011000100001110001111111010000101111001101100110010001010011000011111001
p>

和字符串y在二进制中是 - > 010101101100110110001100010010000101001011001111001001001011000100011000001000110000100001101111100100100010010000111010000111010



但是现在我卡住了,怎么能xor两个字符串来查找匹配位的数量?如何计算它们?



无论我使用Java还是C ++,任何人都可以帮助请求



谢谢

解决方案

在Java中,这非常简单。

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

返回aNumber.xor(bNumber).bitCount();
}

在C ++中,您可以使用bitset。你要找的是海明威体重



如果您真的想不使用BigInteger
以两个字符串的4个字符为单位,将它们转换为一个int,并对它们进行异或并计算一位。重复,直到字符串结束。

  public static int numberOfMatchingOnes(String a,String b){
if(a。 length()!= b.length()|| a.length()%4!= 0){
抛出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;
}

你可以看看 Java源代码,了解 bitCount()有效。


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,

解决方案

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.

这篇关于比较两个十六进制字符串以查找匹配位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆