且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在字节数组中搜索

更新时间:2023-11-09 23:00:04

" Frank" < fr ******* @ advitronic.nlwrote in message

news:13 ************ @ corp.supernews.com ...
"Frank" <fr*******@advitronic.nlwrote in message
news:13************@corp.supernews.com...

我有一个字节数组(byte []),想要搜索每个包含0x10,0x12和0x16的每个

的3个字节。有什么办法吗?
I have a byte array (byte[]) and want to search for 3 bytes next to each
other containing 0x10, 0x12 and 0x16. Is there some method for that?



否简单办法。由于数组没有任何类型的索引,因此任何

搜索方法最终都会循环遍历数组的内容。所以你

也可以自己写一下搜索:


byte [] ba = ...

for(int i = 0; i< ba.Length-2; ba ++)

{

if(ba [i] == 0x10&& ba [i + 1] = = 0x12&& ba [i + 2] == 0x16)

{

//在位置i找到。

}

}

No "easy" way. Since the array does not have any type of indexes, any
search method will ultimately loop through the contents of the array. So you
might as well write the search yourself:

byte[] ba = ...
for (int i=0; i<ba.Length-2; ba++)
{
if (ba[i]==0x10 && ba[i+1]==0x12 && ba[i+2]==0x16)
{
//Found at position i.
}
}


谢谢Alberto,

但这不是我希望的答案当然我想到了这个

解决方案......我正在寻找类似''包含''的东西。

问候

Frank

" Alberto Poblacion" < ea ****************************** @ poblacion.orgwro te
消息新闻中的
: uU ************** @ TK2MSFTNGP04.phx.gbl ...
Thanks Alberto,
but it''s not the answer I was hoping for, of course I thought of this
solution.. I was looking for something like ''contains''.
Regards
Frank
"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:uU**************@TK2MSFTNGP04.phx.gbl...

" Frank" < fr ******* @ advitronic.nlwrote in message

news:13 ************ @ corp.supernews.com ...
"Frank" <fr*******@advitronic.nlwrote in message
news:13************@corp.supernews.com...

>我有一个字节数组(byte []),想要搜索每个包含0x10,0x12和0x16的其他3个字节。有什么办法吗?
>I have a byte array (byte[]) and want to search for 3 bytes next to each
other containing 0x10, 0x12 and 0x16. Is there some method for that?



否简单办法。由于数组没有任何类型的索引,因此任何

搜索方法最终都会循环遍历数组的内容。所以

你也可以自己写一下搜索:


byte [] ba = ...

for(int i = 0; i< ba.Length-2; ba ++)

{

if(ba [i] == 0x10&& ba [i + 1] = = 0x12&& ba [i + 2] == 0x16)

{

//在位置i找到。

}

}


No "easy" way. Since the array does not have any type of indexes, any
search method will ultimately loop through the contents of the array. So
you might as well write the search yourself:

byte[] ba = ...
for (int i=0; i<ba.Length-2; ba++)
{
if (ba[i]==0x10 && ba[i+1]==0x12 && ba[i+2]==0x16)
{
//Found at position i.
}
}



Frank写道:
Frank wrote:

谢谢Alberto,

但这不是我希望的答案,当然我想到了这个

解决方案..我正在寻找像''包含的东西''。

问候

Frank
Thanks Alberto,
but it''s not the answer I was hoping for, of course I thought of this
solution.. I was looking for something like ''contains''.
Regards
Frank



您可以使用Array.IndexOf方法找到一个值。

You can use the Array.IndexOf method to locate one value.


" Alberto Poblacion" < ea ****************************** @ poblacion.orgwro te
消息新闻中的
: uU ************** @ TK2MSFTNGP04.phx.gbl ...
"Alberto Poblacion" <ea******************************@poblacion.orgwro te
in message news:uU**************@TK2MSFTNGP04.phx.gbl...

>" Frank" < fr ******* @ advitronic.nlwrote in message
新闻:13 ************ @ corp.supernews.com ...
>"Frank" <fr*******@advitronic.nlwrote in message
news:13************@corp.supernews.com...

>>我有一个字节数组(byte []),想要搜索每个包含0x10,0x12和0x16的其他3个字节。有什么办法吗?
>>I have a byte array (byte[]) and want to search for 3 bytes next to each
other containing 0x10, 0x12 and 0x16. Is there some method for that?


否简单办法。由于数组没有任何类型的索引,因此任何搜索方法最终都会循环遍历数组的内容。所以你不妨自己写一下搜索:

byte [] ba = ...
for(int i = 0; i< ba.Length-2; ba ++ )
{
if(ba [i] == 0x10&& ba [i + 1] == 0x12&& ba [i + 2] == 0x16)
{
//在我的位置找到。
}
}

No "easy" way. Since the array does not have any type of indexes, any
search method will ultimately loop through the contents of the array. So
you might as well write the search yourself:

byte[] ba = ...
for (int i=0; i<ba.Length-2; ba++)
{
if (ba[i]==0x10 && ba[i+1]==0x12 && ba[i+2]==0x16)
{
//Found at position i.
}
}



-

G?ran Andersson

_____
http:// www .guffa.com