如何在 .NET 中的较大集合中定位值序列(特别是字节)
我需要解析文件中的字节,以便仅在识别出特定的字节序列后才获取数据。例如,如果序列只是 0xFF(一个字节),那么我可以在集合上使用 LINQ:
byte[] allBytes = new byte[] {0x00, 0xFF, 0x01};
var importantBytes = allBytes.SkipWhile(byte b => b != 0xFF);
// importantBytes = {0xFF, 0x01}
但是有没有一种优雅的方法来检测多字节序列 - 例如 0xFF、0xFF - 特别是在它开始时回溯的方法获得误报匹配?
I need to parse the bytes from a file so that I only take the data after a certain sequence of bytes has been identified. For example, if the sequence is simply 0xFF (one byte), then I can use LINQ on the collection:
byte[] allBytes = new byte[] {0x00, 0xFF, 0x01};
var importantBytes = allBytes.SkipWhile(byte b => b != 0xFF);
// importantBytes = {0xFF, 0x01}
But is there an elegant way to detect a multi-byte sequence - e.g. 0xFF, 0xFF - especially one that backtracks in case it starts to get a false positive match?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道任何内置的方式;像往常一样,您始终可以编写自己的扩展方法。这是我的想法(可能有更有效的方法来实现它):
我必须检查以确保这是正确的,但它应该为您提供基本的想法;迭代元素,跟踪检索到的最后一个值序列,找到序列时设置一个标志,设置标志后,开始返回每个后续元素。
编辑-我确实运行了测试,它确实工作正常。这是一些测试代码:
I'm not aware of any built-in way; as per usual, you can always write your own extension method. Here's one off the top of my head (there may be more efficient ways to implement it):
I'll have to check to make sure that this is correct, but it should give you the basic idea; iterate through the elements, track the last sequence of values retrieved, set a flag when the sequence is found, and once the flag is set, start returning each subsequent element.
Edit - I did run a test, and it does work correctly. Here's some test code:
如果将字节转换为字符串,则可以利用其中内置的无数搜索函数,即使您正在使用的字节实际上不是传统意义上的字符。
If you convert your bytes into a string, you can take advantage of the myriad of searching functions built into that, even if the bytes you're working with aren't actually characters in the traditional sense.
就像一点理论一样;这是一个常见的语言问题。您也许可以使用正则表达式引擎来检测它。第一个谷歌搜索“流中的正则表达式”找到
http://codeguru。 Earthweb.com/columns/experts/article.php/c14689
Just as a bit of theory; this is a regular language problem. You may be able to use a regular expression engine to detect it. The first google hit for "regular expression on stream" found
http://codeguru.earthweb.com/columns/experts/article.php/c14689