繁体   English   中英

C# 字节 [十六进制] 数组到字符串 - 无转换

[英]C# Byte[Hex] Array to String - No Conversion

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

 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 的评论是正确的; 您正在将字节转换为字符串以运行搜索算法,该算法将字符串转换为字符数组(即字节数组,即您开始使用的数组)以完成其工作。 使用 KMP 在字节数组中查找字节数组与在字符串中查找字符串相同

为了证明我的观点,我考虑了一个适用于字符串的 KMP 实现。 在 Geeks For Geeks 找到了一个并将其从处理字符串转换为处理字节,实际上只是编辑方法调用中的类型; string 有一个 Length 并且可以像数组一样被索引,byte array 有一个 Length 并且可以被索引,因为它是一个数组等 - 不再需要使这个版本工作:

// 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 的正确实现,可以充分满足您的用例。 它似乎有效,但我只是指出您不需要转换为字符串并再次返回来操作从根本上可以以与搜索字符串完全相同的方式搜索字节的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM