从字节数组中获取具有不同字节的字节模式的所有出现?

发布于 2024-11-08 20:45:35 字数 398 浏览 4 评论 0原文

如何将字节数组与更大的字节数组匹配并获取唯一数据以及模式结束的字节数组中的位置?


FF FF FF FF XX XX XX XX FF FF FF FF (此处包含任何字节的任何长度) 2E XX XX XX 00


我有上述模式(其中 XX 是任何byte) 并且我需要获取粗体部分加上最后一个字节在数组中的位置,我该怎么做? (注意:我需要获取此模式的所有出现位置)

我无法将其转换为字符串,因为它具有空字节 (0x00),并且它们通常是前四个 XX XX XX XX 字节的一部分。

我已经尝试解决这个问题有一段时间了,如果你们能帮助我,我将不胜感激!谢谢。

编辑:以上字节为十六进制

How can I match a byte array to a larger byte array and get the unique data and the location in the byte array that the pattern ends?


FF FF FF FF XX XX XX XX FF FF FF FF (any length of any bytes goes here) 2E XX XX XX 00


I have the above pattern (where XX is any byte) and i need to get bolded parts plus the location in the array of the last byte, How can I do this? (note: I need to get all occurrences of this pattern)

I cannot convert it to a string as it has null bytes (0x00) and they are often part of the first four XX XX XX XX bytes.

I've been trying to figure this out for a while now and would appreciate it if you guys could help me out! Thanks.

Edit: the above bytes are in hex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚我心虐我身 2024-11-15 20:45:35

谁说不能将其转换为字符串?

byte[] bytes = new byte[]
{
    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 0xff, 0x2a, 0x00
};
var s = Encoding.Default.GetString(bytes);
Console.WriteLine(bytes.Length);
Console.WriteLine(s.Length);
foreach (var c in s)
{
    Console.Write("0x{0:X2}, ", (int)c);
}
Console.WriteLine();

数组和字符串的长度都显示为13。字符串输出的字节与数组中的字节相同。

可以将其转换为字符串。然后您可以使用正则表达式来查找您要查找的内容。

请注意,Encoding.Default 可能不是您要查找的内容。您需要一个不修改任何字符的 8 位编码。

但如果你想要一种算法方法来做到这一点,有几种方法会浮现在你的脑海中。第一种方法(可能也是最简单的)是向前扫描,查找 2E 后跟三个字节,然后是 00。然后再次从头开始,看看是否找到 FF FF FF FF XX XX XX XX FF FF FF FF。这不是最快的做事方式,但是非常简单。

请注意,如果您从 2E 向后搜索,您最终可能会“找到”更短的字符串。也就是说,如果您的输入是:

FF FF FF FF XX XX XX XX FF FF FF FF 01 02 FF FF FF FF XX XX XX XX FF FF FF FF 0A 0B 2E XX XX XX 00

有起始模式出现两次。如果您从 2E 向后搜索,您将匹配第二个,这可能不是您想要的。

另一种方法是为自己构建一个向前搜索的小型状态机。这会更快,但也更困难一些。

Who says you can't convert it to a string?

byte[] bytes = new byte[]
{
    0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 0xff, 0x2a, 0x00
};
var s = Encoding.Default.GetString(bytes);
Console.WriteLine(bytes.Length);
Console.WriteLine(s.Length);
foreach (var c in s)
{
    Console.Write("0x{0:X2}, ", (int)c);
}
Console.WriteLine();

Both the array and the string are shown with a length of 13. And the bytes output from the string are the same as the bytes in the array.

You can convert it to a string. Then you can use regular expressions to find what you're looking for.

Note that Encoding.Default might not be what you're looking for. You want an 8-bit encoding that doesn't modify any of the characters.

But if you want an algorithmic way to do it, there are a couple of ways that spring to mind. First way (and probably easiest) is to scan forward looking for 2E followed by three bytes, and then a 00. Then start at the beginning again and see if you find FF FF FF FF XX XX XX XX FF FF FF FF. That's not the fastest way to do things, but it's pretty easy.

Note that if you search backwards from the 2E, you could end up "finding" a shorter string. That is, if your input was:

FF FF FF FF XX XX XX XX FF FF FF FF 01 02 FF FF FF FF XX XX XX XX FF FF FF FF 0A 0B 2E XX XX XX 00

There are two occurrences of the starting pattern. If you searched backwards from the 2E, you'd match the second one, which probably isn't what you want.

The other way is to build yourself a little state machine that searches forward. That'll be faster, but a bit more difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文