【问题标题】:How to get string by FileStream.Position and length specified in c#如何通过 C# 中指定的 FileStream.Position 和长度获取字符串
【发布时间】:2025-12-18 01:30:01
【问题描述】:

我有在大型二进制文件中搜索字符串并将其位置提供给我的函数。 如何实现读取该位置并在特定长度后给我字符串的函数。 正如我们在 String.Substring()

中所做的那样

这是我目前的代码。

public void example()
{

    string match = "400000002532"; //This is 12 chars in hex of the string to search
    byte[] matchBytes = StringToByteArray(match);


    foreach (var jsFile in jsscan)
    {
        using (var fs = new FileStream(jsFile, FileMode.Open))
        {
            int i = 0;
            int readByte;
            while ((readByte = fs.ReadByte()) != -1)
            {
                if (matchBytes[i] == readByte)
                {
                    i++;
                }
                else
                {
                    i = 0;
                }
                if (i == matchBytes.Length)
                {
                    Console.WriteLine("It found between {0} and {1}.", 
                       fs.Position - matchBytes.Length, fs.Position);
                    break;
                }
            }
       }
    }
}
public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
 }

下面是我正在搜索的示例

400000002532010953667A51E44BE5B6A59417B71F4B91BBE590B6AF6E84EF570C32C56400E05123B0A44AF389331E4B91B02E8980B85157F910D7238918A73012B6243F772F7B60E5A7CF6E8CB25374B8FF96311130AABD9F71A860C904C9F6AE9706E570CC0E881E997762710EDE8818CCC551BA05579D30C0D53CEBD9BAF0C2E557D7B9D37A9C94A8A9B5FA7FCF7973B0BDA88A06DE1AE357130E4A06018ABB0A1ABD818DABEB518649CF885953EE05564FD69F0E2F860175667C5FC84F1C97727CEA1C841BFA86A26BABA942E0275FAB2A8F78132E3A05404F0DCD02FD4E7CAD08B1FFD4C2184400F22F6EBC14857BCC2E2AF858BE20CBB807C3467A91E38F31901FD452B5F87F296174631980E039CAB58D97E8F91E3255DD7DEF3177D68A4943F629A70B421B1D6E53DC0D26A1B5EF7C6912F48E0842037FA72B17C18E11B93AEE4DDA0FFE6F217BD5DEB957B1C26169029DE4396103D1F89FA0856489B1958DE5C896DB8F27A24C21AC66BF2095E383DA5EC6DA7138FE82C62FDE9BEFF0308F507736F1B35B1CA083F6C96A6860889BDCCBC989E86F4FB1C483E71557369E7308450330AEF8C9A13A115E8A97642E4A0A4098F5BC04A096A22E5F97116B59AE17BCAEFD2A8B0BCB5341EC64CA3E474900D5A8A620448A6C97827C42332C4DD326572A3C5DB4DA1362F3C0012E1AA1B70C812DCCAEF74F67E94E907518CA31945DD56A61A7

【问题讨论】:

  • 所以你不想知道如何获取位置,你只想知道如何将目标字符串后面的一定数量的字节转换为字符串?如果是这样,你怎么知道下面的字符串应该有多少个字符?
  • 假设您有 utf-16 编码字节,知道起始索引和字节数,使用 Encoding.Unicode.GetString(bytes, start, count) 从字节数组中提取子字符串很简单
  • @IgorBendrup 这并不像您想象的那么简单,因为每个字符可能不完全是两个字节(在 UTF16 中)或每个字符一个字节(在 UTF8 中),因为某些字符可以编码为三个或更多字节。
  • @MatthewWatson 我有一系列十六进制,我想搜索第一个十六进制字符串的长度为 1040 个字符,其他 2 个十六进制字符串长度为 512 个字符。
  • @MatthewWatson 如果 OP 知道他想要转换为字符串的字节(而不是字符)的确切计数,那是没有意义的。如果 OP 不知道字节数,但只知道字符数,他可以迭代地使用 Encoding.GetCharCount(bytes, start, byteCount) 来获得正确的字节数

标签: c#


【解决方案1】:

如果性能不是很重要,您可以执行以下操作,这更简单易读

using (var fs = new StreamReader(fileName))
{
    var content = await fs.ReadToEndAsync();
    var pos = content.IndexOf(matchBytes);

    if (pos != -1)
    {
        Console.WriteLine($"Found @ {pos}, {pos + matchBytes.Length}");
    }
}

【讨论】:

  • 我的意思是我想要的数据在哪里?它只给出位置!
【解决方案2】:

假设你知道Encoding用于存储Stream中的字符,试试这个函数:

static string GetString(Stream stream, long position, int stringLength, Encoding encoding) {
    int offset = 0;
    int readByte;
    byte[] buffer = new byte[stream.Length - position];
    stream.Seek(position, SeekOrigin.Begin);
    while ((readByte = stream.ReadByte()) != -1)
    {
        buffer[offset++] = (byte)readByte;
        if (encoding.GetCharCount(buffer, 0, offset) == stringLength + 1)
        {                    
             return encoding.GetString(buffer, 0, offset - 1);
        }
    }
    if (encoding.GetCharCount(buffer, 0, offset) == stringLength)
    {
        return encoding.GetString(buffer, 0, offset);
    }
    throw new Exception(string.Format("Stream doesn't contains {0} characters", stringLength));
}

例如,使用您的代码和 utf-16:

using (var fs = new FileStream(jsFile, FileMode.Open))
{
    int i = 0;
    int readByte;
    while ((readByte = fs.ReadByte()) != -1)
    {
        if (matchBytes[i] == readByte)
        {
            i++;
        }
        else
        {
            i = 0;
        }
        if (i == matchBytes.Length)
        {
            Console.WriteLine("It found between {0} and {1}.",
                        fs.Position - matchBytes.Length, fs.Position);

            //Desired string length in charachters
            const int DESIRED_STRING_LENGTH = 5;
            Console.WriteLine(GetString(fs, fs.Position, DESIRED_STRING_LENGTH, Encoding.Unicode));

            break;
        }
    }
}

【讨论】:

  • 这件事变得非常棘手,因为我不知道编码。
  • @AmanAli 编码检测是非常不同的任务。查看this。但最好向文件作者询问使用的编码。
  • 作者不详。
最近更新 更多