C# 如何从字节数组中提取字节?已知起始字节
C# How to extract bytes from byte array? With known starting byte
我需要从字节数组中获取特定字节。我知道我想要的第一个字节的内容,之后我需要 x 个字节。
比如我有
byte [] readbuffer { 0, 1, 2, 0x55, 3, 4, 5, 6};
byte [] results = new byte[30];
我需要出现在“0x55”之后的 3 个字节
byte results == {ox55aa, 3, 4, 5}
我正在使用:
Array.copy(readbuffer, "need the index of 0x55" ,results, 0, 3);
我需要找到0x55的索引
PS:0x55
在数组中处于任意位置。
PS2: 之前忘记说了,我是在.Net Micro Framework 工作的。
(对于非代码描述,我很抱歉,我是编程的新手...和英语)
提前致谢
[编辑]x2
可以这样实现:
byte[] bytes = new byte[] { 1, 2, 3, 4, 0x55, 6, 7, 8 };
byte[] newBytes = new byte[4];
Buffer.BlockCopy(bytes,Array.IndexOf(bytes,(byte)0x55), newBytes,0,4);
我想您只需要在整个数组中搜索特定值并记住找到它的索引...
int iIndex = 0;
for (; iIndex < valuearray.Length; iIndex++);
if (valuearray[iIndex] == searchedValue) break;
然后从这里开始用找到的索引做你想做的事。
P.S。也许有轻微的语法错误,因为我通常使用 C++.net
byte[] results = new byte[16];
int index = Array.IndexOf(readBuffer, (byte)0x55);
Array.Copy(readBuffer, index, results, 0, 16);
谢谢大家
现在这是我的代码。一切如我所愿:)
我需要从字节数组中获取特定字节。我知道我想要的第一个字节的内容,之后我需要 x 个字节。
比如我有
byte [] readbuffer { 0, 1, 2, 0x55, 3, 4, 5, 6};
byte [] results = new byte[30];
我需要出现在“0x55”之后的 3 个字节
byte results == {ox55aa, 3, 4, 5}
我正在使用:
Array.copy(readbuffer, "need the index of 0x55" ,results, 0, 3);
我需要找到0x55的索引
PS:0x55
在数组中处于任意位置。
PS2: 之前忘记说了,我是在.Net Micro Framework 工作的。
(对于非代码描述,我很抱歉,我是编程的新手...和英语)
提前致谢
[编辑]x2
可以这样实现:
byte[] bytes = new byte[] { 1, 2, 3, 4, 0x55, 6, 7, 8 };
byte[] newBytes = new byte[4];
Buffer.BlockCopy(bytes,Array.IndexOf(bytes,(byte)0x55), newBytes,0,4);
我想您只需要在整个数组中搜索特定值并记住找到它的索引...
int iIndex = 0;
for (; iIndex < valuearray.Length; iIndex++);
if (valuearray[iIndex] == searchedValue) break;
然后从这里开始用找到的索引做你想做的事。
P.S。也许有轻微的语法错误,因为我通常使用 C++.net
byte[] results = new byte[16];
int index = Array.IndexOf(readBuffer, (byte)0x55);
Array.Copy(readBuffer, index, results, 0, 16);
谢谢大家
现在这是我的代码。一切如我所愿:)