在字节数组中搜索 [英] search in byte array

查看:81
本文介绍了在字节数组中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

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

的3个字节。有没有办法呢?

谢谢

Frank

Hello,
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?
Thanks
Frank

推荐答案

" 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


这篇关于在字节数组中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆