c#-如何从位创建字节?

c#-如何从位创建字节?,c#,bit-manipulation,C#,Bit Manipulation,我正在开发一个应用程序,可以通过RTP发送数据,但我几乎从未使用过字节。现在我正在尝试使用位数组: byte[] toSend = new byte[12]; BitArray b = new BitArray(new int[] { 2 }); b.CopyTo(toSend, 0); 但是它与Int32一起工作,所以2的表示方式类似于0100..0,这不是我需要的。我这里有两个问题: 如何将2 | 1 | 1 | 4位组合成一个字节?我认为应该有这样的东西: int version = 2

我正在开发一个应用程序,可以通过
RTP
发送数据,但我几乎从未使用过字节。现在我正在尝试使用
位数组

byte[] toSend = new byte[12];
BitArray b = new BitArray(new int[] { 2 });
b.CopyTo(toSend, 0);
但是它与
Int32
一起工作,所以2的表示方式类似于
0100..0
,这不是我需要的。我这里有两个问题:

  • 如何将
    2 | 1 | 1 | 4
    位组合成一个字节?我认为应该有这样的东西:

    int version = 2;//2 bits
    int padding = 0;//1 bit
    int extension = 0;//1 bit
    int ccrc = 0;//4 bits
    
    byte[] toSend = new byte[1]{version+padding+extension+ccrc};
    
  • 对于某些头,有16位保留,所以我需要一些东西 像这样:
    000000000000000(16)
    ,但我不知道如何创建这种变量,以及如何将16位写入两个字节


  • 如果我理解正确,您正在尝试从位创建一个字节


    为什么不使用一个函数,该函数接受一个字节、位值和所解释的位的位置?

    如果我理解正确,您正在尝试从位创建一个字节


    为什么不使用一个函数,该函数包含一个字节、位值和放置位的位置,如前所述?

    从第二点开始。如何创建仅包含
    0
    的16位(2字节)变量:

    char _16bitsOfZero = '\0'; // this will have the default value of NULL character
    // which basically is 0000 0000 0000 0000 
    
    继续从4个整数创建一个字节值:

    int version = 2; // 2 bits which will be shifted to the most left
    int padding = 0; // 1 bit which will be shifted to the most left but just before version
    int extension = 0; // 1 bit which will be shifted to right before padding
    int ccrc = 0; // 4 bits that ... wont be shifted.
    
    // first of all this all is going to be one byte ( 8 bits )
    // 00 0 0 0000 <- beginning value
    byte b = 0;
    // now we want to shift our ccrc to be in the back
    // 00 0 0 ccrc <- like this
    // to put this value there we need to shift this 
    b = (byte)ccrc;
    // now we want to shift extension right after ccrc value
    // remembering that ccrc is 4 bits wide :
    b |= (extension << 4);
    // now do the same with padding :
    b |= (padding << 5); // 4 because previous value was 1 bit wide
    // now the same with version :
    b |= (version << 6);
    // output from this should be :
    // 10 0 0 0000
    // which is 128
    

    从第二点开始。如何创建仅包含
    0
    的16位(2字节)变量:

    char _16bitsOfZero = '\0'; // this will have the default value of NULL character
    // which basically is 0000 0000 0000 0000 
    
    继续从4个整数创建一个字节值:

    int version = 2; // 2 bits which will be shifted to the most left
    int padding = 0; // 1 bit which will be shifted to the most left but just before version
    int extension = 0; // 1 bit which will be shifted to right before padding
    int ccrc = 0; // 4 bits that ... wont be shifted.
    
    // first of all this all is going to be one byte ( 8 bits )
    // 00 0 0 0000 <- beginning value
    byte b = 0;
    // now we want to shift our ccrc to be in the back
    // 00 0 0 ccrc <- like this
    // to put this value there we need to shift this 
    b = (byte)ccrc;
    // now we want to shift extension right after ccrc value
    // remembering that ccrc is 4 bits wide :
    b |= (extension << 4);
    // now do the same with padding :
    b |= (padding << 5); // 4 because previous value was 1 bit wide
    // now the same with version :
    b |= (version << 6);
    // output from this should be :
    // 10 0 0 0000
    // which is 128
    

    我想这回答了我的第一个问题。但是如何存储16位和32位的值呢?简单的Int16或Int32?您可以创建一个字节数组(这样就不会影响int的大小),也可以让函数返回一个long-long并将结果转换为您需要的结果。我想它回答了我的第一个问题。但是如何存储16位和32位的值呢?简单的Int16或Int32?您可以创建一个字节数组(这样就不会影响int的大小),或者让函数返回一个long-long并将结果强制转换为您需要的值不要忘记检查endianness/如果系统的endianness错误,请添加一个异常!如果系统的endianness错误,不要忘记检查endianness/添加异常!