简体   繁体   English

c#使用低位和高位将两个ushort值转换为一个字节

[英]c# convert two ushort values into one byte using low bits and high bits

I am having a question, I need to convert to two u short numbers lets say 1 and 2 to 1 byte. 我有一个问题,我需要转换为两个u短数字,说1和2到1个字节。 someting like 有点像

0 0 1 0 values of 2 and 0 0 0 1 value of 1 0 0 1 0值2和0 0 0 1值1

so in result i get a byte with value 00100001 , Is it possible, I am not a master low level coder. 所以结果我得到了一个值00100001的字节,可能00100001 ,我不是一个主机低级编码器。

这应该工作:

 (byte)(((value1 & 0xF)<<4) | (value2 & 0xF))

I am not a master low level coder. 我不是高级底层编码器。

Well, now is the time to become one! 好,现在是时候成为一体了!

Edit: this answer was made before the question was clear enough to understand exactly what was required. 编辑:在问题足够清楚以至于不能完全理解所需内容之前就做出了这个答案。 See other answers. 查看其他答案。

Use a 'bit mask' on the two numbers, then bitwise-OR them together. 在两个数字上使用“位掩码”,然后将它们按位“或”在一起。
I can't quite tell how you exactly want it, but let's say you wanted the first 4 bits of the first ushort , then the last 4 bits of the second ushort . 我还不太清楚您到底想要什么,但是假设您想要第一个ushort的前4位,然后是第二个ushort的后4位。 To note: ushort is 16 bits wide. 注意: ushort为16位宽。

ushort u1 = 44828; //10101111 00011100 in binary
ushort u2 = 65384; //11111111 01101000 in binary

int u1_first4bits = (u1 & 0xF000) >> 8;

The 'mask' is 0xF000. “掩码”为0xF000。 It masks over u1: 它掩盖了u1:

44828         1010 1111 0001 1100
0xF000        1111 0000 0000 0000
bitwise-AND   1010 0000 0000 0000

The problem is, this new number is still 16 bits long - we must shift it by 8 bits with >> 8 to make it 问题是,这个新数字仍然是16位长-我们必须将其>> 8移位8位
0000 0000 1010 0000

Then another mask operation on the second number: 然后对第二个数字进行另一个掩码操作:

int u2_last4bits  =  u2 & 0x000F;

Illustrated: 图说:

65384         1111 1111 0110 1000
0x000F        0000 0000 0000 1111
bitwise-AND   0000 0000 0000 1000

Here, we did not need to shift the bits, as they are already where we want them. 在这里,我们不需要移动位,因为它们已经在我们想要的位置。
Then we bitwise-OR them together: 然后我们对它们进行按位或运算:

byte b1 = (byte)(u1_first4bits | u2_last4bits);
//b1 is now 10101000 which is 168

Illustrated: 图说:

u1_first4bits 0000 0000 1010 0000
u2_last4bits  0000 0000 0000 1000
bitwise-OR    0000 0000 1010 1000

Notice that u1_first4bits and u2_first4bits needed to be of type int - this is because C# bitwise operations return int . 请注意, u1_first4bitsu2_first4bits必须为int类型-这是因为C#按位操作返回int To create our byte b1 , we had to cast the bitwase-OR operation to a byte. 要创建byte b1 ,我们必须将bitwase-OR操作强制转换为一个字节。

Assuming, you want to take the 2 ushorts (16 bit each) and convert them to a 32 bit representation (integer), you can use the "BitArray" Class, fill it with a 4 byte array, and convert it to an integer. 假设您要使用2个ushort(每个16位)并将其转换为32位表示形式(整数),则可以使用“ BitArray”类,将其填充为4字节数组,然后将其转换为整数。

The following example will produce: 以下示例将产生:

 00000000 00000010 00000000 00000001

which is 这是

 131073

as integer. 作为整数。

ushort x1 = 1;
ushort x2 = 2;

//get the bytes of the ushorts. 2 byte per number. 
byte[] b1 = System.BitConverter.GetBytes(x1);
byte[] b2 = System.BitConverter.GetBytes(x2);

//Combine the two arrays to one array of length 4. 
byte[] result = new Byte[4];
result[0] = b1[0];
result[1] = b1[1];
result[2] = b2[0];
result[3] = b2[1];

//fill the bitArray.
BitArray br = new BitArray(result);

//test output.
int c = 0;
for (int i = br.Length -1; i >= 0; i--){

    Console.Write(br.Get(i)? "1":"0");
    if (++c == 8)
    {
        Console.Write(" ");
        c = 0;
    }
}

//convert to int and output. 
int[] array = new int[1];
br.CopyTo(array, 0);
Console.WriteLine();
Console.Write(array[0]);

Console.ReadLine();

Of course you can alter this example and throw away 1 Byte per ushort. 当然,您可以更改此示例,并为每个ushort 丢弃 1个字节。 But this wouldn't be a correct "conversion" then. 但这不是正确的“转换”。

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

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