c# Byte[十六进制]数组到字符串-不转换



所以我使用以下代码从内存读取:

public static extern bool ReadProcessMemory(IntPtr handle, IntPtr baseAddress, [Out] byte[] buffer, int size, out IntPtr numberOfBytesRead);

使用上面的代码将内存十六进制代码输出为字节数组:

buffer[0] = 01;
buffer[1] = 2D;
buffer[2] = F2;

我想在一定范围内的十六进制代码搜索某个十六进制数组。要做到这一点,我想使用"模式搜索的KMP算法"。

目前我使用以下代码来实现:

byte[] moduleBytes = {0};
IntPtr bytesRead;
ReadProcessMemory(process.Handle, baseAddress, moduleBytes, moduleBytes.Length, out bytesRead);
string buffer = "";
foreach (byte bytesfrommemory in moduleBytes)
{
buffer += bytesfrommemory.ToString("X");;
}
//algorithm
string data = buffer;
int[] value = SearchString(data, pattern);
foreach (int entry in value)
{
Console.WriteLine(entry); //Outputs the offset where it found the code
}

这样做的问题是循环遍历每个字节以将其添加到缓冲区字符串中需要+1000字节。有没有更快的方法来"转换"?字节数组从数组到字符串没有实际转换它,因为我仍然需要原始字节数组只是字符串?

我尝试用下面的代码,但它转换成不同的东西:

char[] characters = moduleBytes.Select(o => (char)o).ToArray();
string buffer = new string(characters);

谢谢你的帮助:)

Alexei的评论是正确的;您正在将字节转换为字符串以运行搜索算法,该算法将字符串转换为char数组(即字节数组,即您开始的内容),以便完成其工作。使用KMP在字节数组中查找字节数组与在字符串

中查找字符串相同。为了证明我的观点,我四处寻找一个用于字符串的KMP实现。我在Geeks For Geeks上找到了一个,并将它从处理字符串转换为处理字节,实际上只是在方法调用中编辑类型;字符串有一个长度,可以像数组一样被索引,字节数组有一个长度,可以被索引,因为它是一个数组等等-没有更多的需要使这个版本工作:

// C# program for implementation of KMP pattern 
// searching algorithm 
using System; 
public class GFG { 
void KMPSearch(byte[] pat, byte[] txt) 
{ 
int M = pat.Length; 
int N = txt.Length;   
// create lps[] that will hold the longest 
// prefix suffix values for pattern 
int[] lps = new int[M]; 
int j = 0; // index for pat[] 
// Preprocess the pattern (calculate lps[] 
// array) 
computeLPSArray(pat, M, lps); 
int i = 0; // index for txt[] 
while (i < N) { 
if (pat[j] == txt[i]) { 
j++; 
i++; 
} 
if (j == M) { 
Console.Write("Found pattern "
+ "at index " + (i - j)); 
j = lps[j - 1]; 
} 
// mismatch after j matches 
else if (i < N && pat[j] != txt[i]) { 
// Do not match lps[0..lps[j-1]] characters, 
// they will match anyway 
if (j != 0) 
j = lps[j - 1]; 
else
i = i + 1; 
} 
} 
} 
void computeLPSArray(byte[] pat, int M, int[] lps)
{ 
// length of the previous longest prefix suffix 
int len = 0; 
int i = 1; 
lps[0] = 0; // lps[0] is always 0 
// the loop calculates lps[i] for i = 1 to M-1 
while (i < M) { 
if (pat[i] == pat[len]) { 
len++; 
lps[i] = len;  
i++; 
} 
else // (pat[i] != pat[len]) 
{ 
// This is tricky. Consider the example. 
// AAACAAAA and i = 7. The idea is similar 
// to search step. 
if (len != 0) { 
len = lps[len - 1];   
// Also, note that we do not increment 
// i here 
} 
else // if (len == 0) 
{ 
lps[i] = len; 
i++; 
} 
} 
} 
} 

// Driver program to test above function 
public static void Main() 
{ 
string txt = System.Text.Encoding.ASCII.GetBytes("ABABDABACDABABCABAB"); 
string pat = System.Text.Encoding.ASCII.GetBytes("ABABCABAB"); 
new GFG().KMPSearch(pat, txt); 
} 
} 

// This code has been contributed by Amit Khandelwal. 

最大的工作(键入的键数最多)是使用System.Text.Encoding.ASCII.GetBytes获得一对字节数组以输入😀

取点;完全不要转换内存字节-将搜索词转换为字节,然后在内存字节

中搜索它免责声明:我不保证这是KMP的正确实现,能够充分满足您的用例。它似乎是工作的,但我只是指出,你不需要转换为字符串,然后再返回操作代码,基本上可以搜索字节与搜索字符串

完全相同的方式。

最新更新