将一个Byte []组合到单个阵列

时间:2012-12-18 09:19:10

标签: c# arrays buffer readblock

我的字节数组包含一系列数字......

t阻止而不是其余的!

我怎样才能在Temp[]中拥有4-8块?

1 个答案:

答案 0 :(得分:2)

元素4-8(或实际上索引3-7)是5个字节。不是4.
您有源偏移和计数混合:

Buffer.BlockCopy(bResponse, 3, temp, 0, 5);

现在临时将包含[23232]

如果你想要最后4个字节,那么使用它:

Buffer.BlockCopy(bResponse, 4, temp, 0, 4);

现在临时将包含[3232] 要将其转换为int:

if (BitConverter.IsLittleEndian)
  Array.Reverse(temp);

int i = BitConverter.ToInt32(temp, 0);

修改(评论[43323232]实际上是{43, 32, 32, 32}后)

var firstByte = temp[0];   // This is 43
var secondByte = temp[1];  // This is 32
var thirdByte = temp[2];   // 32
var fourthByte = temp[3];  // 32

如果要将其转换为int,则上面的BitConverter示例仍可正常工作。

相关问题