简体   繁体   English

C#对十六进制值的二进制运算

[英]c# Binary operation on hex values

I have a hex value that eg following octets: 82 A9 43 我有一个十六进制值,例如以下八位字节:82 A9 43

I need to remove the 8 bit of all of them and concatenate them together. 我需要删除所有8位并将它们连接在一起。

eg: if we change each to binary: 例如:如果我们将每个都更改为二进制:

82 = 10000010
A9 = 10101001
43 = 01000011

Now I need to remove the MSB on all of the above with the following code: 现在,我需要使用以下代码删除所有上述内容的MSB:

                    int t = 0x7F;
                    int hv1 = 0x82;
                    int hv2 = 0xA9;
                    int hv3 = 0x43;

                    int r1 = hv1 & t;
                    int r2 = hv2 & t;
                    int r3 = hv3 & t;

After the above procedure it looks like: 完成上述过程后,它看起来像:

r1 = 10
r2 = 101001
r3 = 100011

But I need as follows: 但我需要如下:

r1 = 0000010
r2 = 0101001
r3 = 0100011

The reason is later I wanted concatenate all the r1 | 原因是以后我想连接所有r1 | r2 | r2 | r3 to be as follows: r3如下:

000001001010010100011

The above should then be converted to integer. 然后应将以上转换为整数。

Do you have a clue, how can this be done. 你有一个线索,怎么做。 Thank you for your help. 谢谢您的帮助。

Leading 0's on binary values don't really do anything, so you can just count the characters and add the 0's back to the front of them, and then concatenate them together 在二进制值上以0开头实际上并没有执行任何操作,因此您可以对字符进行计数,然后将0的后面加回到它们的前面,然后将它们连接在一起

string PadLeft (string s) {
   return string.Format("({0,8})", s)
}

It is not clear how you are converting your integer values into the binary strings, but I imagine you are doing something like this: 目前尚不清楚如何将整数值转换为二进制字符串,但我想您正在执行以下操作:

string binaryR1 = Convert.ToString(r1, 2);//binaryR1 = "10"

You can then pad the string using the PadLeft() function: 然后,您可以使用PadLeft()函数填充字符串:

binaryR1 = binaryR1.PadLeft(7, '0');//binaryR1 = "0000010"

or in a single line: 或一行:

string binaryR1 = Convert.ToString(r1, 2).PadLeft(7, '0');//binaryR1 = "0000010"

NOTE: These values have been padded to 7 digits as you do not require the MSB 注意:这些值已填充为7位,因为您不需要MSB

Applying this to all 3 values would then allow you to concat into the full string: 然后将其应用于所有3个值,即可连接到完整字符串:

string full = binaryR1 + binaryR2 + binaryR3;//full = "000001001010010100011"

There is no need to convert to strings then back to integers, just do integer math like this: 无需转换为字符串然后再返回整数,只需执行如下整数运算:

int concat = r1 << 16 | r2 << 8 | r3;

or more briefly: 或更简短地说:

int ttt = 0x7f7f7f; 
int concat = (hv1 << 16 | hv2 << 8 | hv3 & ttt;

Numerically, leading zeroes are irrelevant. 在数值上,前导零无关紧要。 Have your function return a String representation of the number so you can pad it to 8 characters. 让您的函数返回数字的字符串表示形式,以便您可以将其填充为8个字符。

if you want them as you've described them, just tack on some zero's to the front, it won't change the binary value 如果您要像描述它们时那样使用它们,只需在前面加上一些零,就不会更改二进制值

private static string AddZeros(string r)
    {
        while (r.Length < 8)
            r = "0" + r;

        return r;
    }

Try something like this: 尝试这样的事情:

int r1 = hv1 & 0x7F;
int r2 = hv2 & 0x7F;
int r3 = hv3 & 0x7F;
int res = ( r1 << 14 ) | ( r2 << 7 ) | r3;

That should strip the 8th bit of each byte, then concatenate bit 0-7 of each into a 21-bit number: 那应该剥离每个字节的第8位,然后将每个字节的位0-7连接到21位数字中:

111111122222223333333 // where 1 are bits from r1, 2 from r2 and so on

Is that what you wanted? 那是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM