我有在大型二进制文件中搜索字符串并将其位置给我的函数。如何实现读取该位置并在特定长度后给我字符串的函数。正如我们在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; }我正在搜索的示例如下400000002532010953667A51E44BE5B6A59417B71F4B91BBE590B6AF6E84EF570C32C56400E05123B0A44AF389331E4B91B02E8980B85157F910D7238918A73012B6243F772F7B60E5A7CF6E8CB25374B8FF96311130AABD9F71A860C904C9F6AE9706E570CC0E881E997762710EDE8818CCC551BA05579D30C0D53CEBD9BAF0C2E557D7B9D37A9C94A8A9B5FA7FCF7973B0BDA88A06DE1AE357130E4A06018ABB0A1ABD818DABEB518649CF885953EE05564FD69F0E2F860175667C5FC84F1C97727CEA1C841BFA86A26BABA942E0275FAB2A8F78132E3A05404F0DCD02FD4E7CAD08B1FFD4C2184400F22F6EBC14857BCC2E2AF858BE20CBB807C3467A91E38F31901FD452B5F87F296174631980E039CAB58D97E8F91E3255DD7DEF3177D68A4943F629A70B421B1D6E53DC0D26A1B5EF7C6912F48E0842037FA72B17C18E11B93AEE4DDA0FFE6F217BD5DEB957B1C26169029DE4396103D1F89FA0856489B1958DE5C896DB8F27A24C21AC66BF2095E383DA5EC6DA7138FE82C62
2 回答
慕的地8271018
TA贡献1796条经验 获得超4个赞
如果性能不是很大的问题,您可以执行以下操作,这更容易和可读
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 回答
- 0 关注
- 314 浏览
添加回答
举报
0/150
提交
取消