本文介绍了比较掩蔽字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较掩蔽2字节数组。所以,我有数据的DataTemplate:

 字节[]数据=新的字节[] {0x3b,0xFE时,为0x18,0x00时,为0x00,0x80的,0X31,0xFE的,
                           ×45,×45(0x73)的,0x74,0x75,0x49,0x44进行,为0x20,
                           0x76,0x65,0x72,0x20的,0X31,0x2E之间,为0x30,0xa8};字节[] = DataTemplate中新的字节[] {0x66,0xFE时,为0x18,0x00时,为0x00,0x80的,0X31,0xFE的,
                                   ×45,×45(0x73)的,0x74,0x75,0x49,0x44进行,为0x20,
                                   0x76,0x65,0x72,0x20的,0X31,0x2E之间,为0x30,0xa8};

和我有面膜:

 字节[] =面膜新的字节[] {0×00,0×00,0×00,0×00,0×00,0×00,0×00,0×00,
                           为0x00,0xFF的,为0xFF,0xFF的,为0xFF,0xFF的,为0xFF,0xFF的,
                           为0xFF,0xFF的,为0xFF,0xFF的,为0xFF,0xFF的,为0xFF,0×00};

所有字节什么 0×00 可chanaged和 0xFF的着。所以,当我比较数据的DataTemplate然后让说数据[0] 0x3b 在一个阵列和别的东西另一个。但数据[9] 必须在两个相同的。现在我做的是这样的:

 列表<位> maskedDataList =新的List<位>();的for(int i = 0; I< data.Length;我++)
{
    字节maskedByte =(字节)((DataTemplate中[1] - 放大器;掩盖[I]));
    atrList.Add(maskedByte);
}的for(int i = 0; I< data.Length;我++)
{
    如果((数据[1] - 安培;!maskedDataList [I])= MaskedDataList [I])
    {
        抛出新的异常(阵列不匹配!);
    }
}

不过,这看起来像矫枉过正。也许有更好的方式这样做呢?

谢谢!


解决方案

 的for(int i = 0; I< data.Length;我++)
    {
        如果(屏蔽[I] == 0xFF的&放大器;&安培;!数据[I] =的DataTemplate [I]){
            抛出新的异常(阵列不匹配!);
        }
    }

I want to compare 2 byte arrays with masking. So I have data and dataTemplate:

byte[] data = new byte[] { 0x3b, 0xfe, 0x18, 0x00, 0x00, 0x80, 0x31, 0xfe,
                           0x45, 0x45, 0x73, 0x74, 0x75, 0x49, 0x44, 0x20,
                           0x76, 0x65, 0x72, 0x20, 0x31, 0x2e, 0x30, 0xa8 };

byte[] dataTemplate = new byte[] { 0x66, 0xfe, 0x18, 0x00, 0x00, 0x80, 0x31, 0xfe,
                                   0x45, 0x45, 0x73, 0x74, 0x75, 0x49, 0x44, 0x20,
                                   0x76, 0x65, 0x72, 0x20, 0x31, 0x2e, 0x30, 0xa8 };

And I have mask:

byte[] mask = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                           0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                           0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };

All bytes what are 0x00 can be chanaged and 0xFF cant. So when I compare data and dataTemplate then lets say data[0] can be 0x3b in one array and something else in the other. But data[9] has to be same in both. Right now I'm doing it like this:

List<byte> maskedDataList = new List<byte>();

for (int i = 0; i < data.Length; i++ )
{
    byte maskedByte = (byte)((dataTemplate[i] & mask[i]));
    atrList.Add(maskedByte);
}

for (int i = 0; i < data.Length; i++)
{
    if ((data[i] & maskedDataList[i]) != MaskedDataList[i])
    {
        throw new Exception("arrays dont match!");
    }
}

But this looks like overkill. Maybe there is better ways doing this?

Thanks!

解决方案
    for (int i = 0; i < data.Length; i++ )
    {
        if (mask[i] == 0xFF && data[i] != dataTemplate[i]) {
            throw new Exception("arrays dont match!");
        }
    }

这篇关于比较掩蔽字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 15:42