[英]C# Find offset of byte pattern, check specific byte, change byte, export part of byte array
This could be long one. 这可能很长。 I do have a binary file, that contains some information. 我确实有一个二进制文件,其中包含一些信息。
What I want to do: 我想做的事:
So, here're my codes for every step. 因此,这是我每一步的代码。 Step one : 1. 步骤一 :1。
Byte[] bytes;
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
path = ofd.FileName;
bytes = File.ReadAllBytes(path);
Step two , search specific pattern in this file. 第二步 ,在该文件中搜索特定模式。 I used some help here on Stackoverflow, and end up with this: 我在Stackoverflow上使用了一些帮助,最后得到了以下结果:
VOID from stackoverflow: 来自stackoverflow的VOID:
static public List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
{
List<int> positions = new List<int>();
int patternLength = pattern.Length;
int totalLength = bytes.Length;
byte firstMatchByte = pattern[0];
for (int i = 0; i < totalLength; i++)
{
if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
{
byte[] match = new byte[patternLength];
Array.Copy(bytes, i, match, 0, patternLength);
if (match.SequenceEqual<byte>(pattern))
{
positions.Add(i);
i += patternLength - 1;
}
}
}
return positions;
}
My void to search for pattern: 我无法搜索模式:
void CheckCamera()
{
Byte[] szukajkamera = { 0x02, 0x00, 0x08, 0x00, 0x20};
List<int> positions = SearchBytePattern(szukajkamera, bytes);
foreach (var item in positions){
MessageBox.Show(item.ToString("X2"));
IndexCamera = item;
}
int OffsetCameraCheck = IndexCamera + 2;
}
Item is now my offset, where 02 00 08 00 20 is in file. 现在,项目是我的偏移量,其中文件02 00 08 00 20。 Now, how do I check, if bytes(offset=IndexCamera+2) == 0x08 ? 现在,如何检查bytes(offset = IndexCamera + 2)== 0x08? I can do array.IndexOf, but there's plenty of 08 before that 08 I'm looking for. 我可以执行array.IndexOf,但是在寻找08之前有很多08。
For step 5 I'm also doing the thing, but it gets impossible for me, when Buffer.BlockCopy ask me for length. 对于第5步,我也正在执行此操作,但是当Buffer.BlockCopy向我询问长度时,对我而言这是不可能的。 For step 5 and forward I need to search again in this same file for another pattern, get it's offset and copy from that offset till the end. 对于第5步及以后的操作,我需要在同一文件中再次搜索另一个模式,获取它的偏移量并从该偏移量复制到最后。 If I want so, then I need to buffer.blockcopy to non-empty byte array, but I just need it empty! 如果需要的话,我需要将buffer.blockcopy缓存到非空字节数组,但是我只需要将其清空即可! I totally lost it. 我完全失去了它。 Please, help me. 请帮我。 Thank you! 谢谢!
When doing pattern searching the above answer does work, however you need to adapt it to search for more of the pattern. 在进行模式搜索时,上述答案确实有效,但是您需要对其进行调整以搜索更多模式。
Eg: If you are looking for the location of 08 1D 1A AA 43 88 33 例如:如果您要查找08 1D 1A AA 43 88 33的位置
then you would need something like: 那么您将需要以下内容:
public static unsafe long IndexOf(this byte[] haystack, byte[] needle, long startOffset = 0)
{
fixed (byte* h = haystack) fixed (byte* n = needle)
{
for (byte* hNext = h + startOffset, hEnd = h + haystack.LongLength + 1 - needle.LongLength, nEnd = n + needle.LongLength; hNext < hEnd; hNext++)
for (byte* hInc = hNext, nInc = n; *nInc == *hInc; hInc++)
if (++nInc == nEnd)
return hNext - h;
return -1;
}
}
Note : Credit to Dylan Nicholson who wrote this code. 注意:感谢编写此代码的Dylan Nicholson。
我该怎么做bytes(offset = IndexCamera + 2)== 0x08?
if(bytes[IndexCamera+2] == 0x08)....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.