Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-读取二进制十六进制字节序列_C#_Byte_Binary Search - Fatal编程技术网

C#-读取二进制十六进制字节序列

C#-读取二进制十六进制字节序列,c#,byte,binary-search,C#,Byte,Binary Search,所以我一直在谷歌上搜索,但我找不到解决我的问题的办法。我可以找到关于字节数组的东西。但我希望我的案例也有一个更简单的解决方案。 也许只是我用错了搜索词,我不知道 无论如何,我已经有了一种工作代码: static void Main(string[] args) { // Open the file to search in BinaryReader br = new BinaryReader(File.OpenRead("D:/Users/Joey

所以我一直在谷歌上搜索,但我找不到解决我的问题的办法。我可以找到关于字节数组的东西。但我希望我的案例也有一个更简单的解决方案。 也许只是我用错了搜索词,我不知道

无论如何,我已经有了一种工作代码:

    static void Main(string[] args)
    {
        // Open the file to search in
        BinaryReader br = new BinaryReader(File.OpenRead("D:/Users/Joey/Desktop/prod"));
        for (int i = 0; i <= br.BaseStream.Length; i++)
        {
            // Search the file for the given byte
            if (br.BaseStream.ReadByte() == (byte)0xC0)
            {
                Console.WriteLine("Found the byte at offset " + i); //write to the console on which offset it has been found
            }
        }
    }
static void Main(字符串[]args)
{
//打开要搜索的文件
BinaryReader br=新的BinaryReader(File.OpenRead(“D:/Users/Joey/Desktop/prod”);

对于(int i=0;i尝试一下。您需要验证数组是否正确。在二进制流中,字节数组只是从
offset
开始的字节集合,其大小为
count
字节

//here is where you initialize your array. you may need to tweak the values to match your byte range (array)
byte[] dataArray = new byte[9] { 0x93, 0x0E, 0x40, 0xF9, 0x53, 0x00, 0x00, 0xB5, 0xDE };

//here is where you initialize the NEW array you want to write where your matching array lives
byte[] newArray = new byte[9] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

// Open the file to search in
BinaryReader br = new BinaryReader(File.OpenRead("D:/Users/Joey/Desktop/prod"));
for (int i = 0; i <= br.BaseStream.Length; i++)
{
    // Search the file for the STARTING byte of my match
    if (br.BaseStream.ReadByte() == (byte)0x93)
    {
        Console.WriteLine("Found the starting byte at offset " + i); //write to the console on which offset it has been found
        byte[] tempArray = new byte[9];
        tempArray = br.ReadBytes(9);
        //now compare the arrays to see if you have a full match:
        int matched = 0;
        for (int j=0; j<tempArray.Length; j++)
        {
            if(tempArray[j] == dataArray[j])
            {
                 matched++;
            }
        }

        //if the arrays match, write your new values:
        if(matched == tempArray.Length-1)
        {
            br.BaseStream.Write(newArray, i, 9);
            break; //exit the loop when finished                    
        }                    
    }
}
//这里是初始化数组的地方。您可能需要调整值以匹配字节范围(数组)
byte[]dataArray=新字节[9]{0x93、0x0E、0x40、0xF9、0x53、0x00、0x00、0xB5、0xDE};
//这里是初始化要写入的新数组的位置,匹配数组所在的位置
byte[]newArray=新字节[9]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//打开要搜索的文件
BinaryReader br=新的BinaryReader(File.OpenRead(“D:/Users/Joey/Desktop/prod”);

对于(int i=0;i您可以使用此扩展来搜索AOB:

public static class StreamExtensions
{
    public static IEnumerable<long> ScanAOB(this Stream stream, params byte[] aob)
    {
        long position;
        byte[] buffer = new byte[aob.Length - 1];

        while ((position = stream.Position) < stream.Length)
        {
            if (stream.ReadByte() != aob[0]) continue;
            if (stream.Read(buffer, 0, aob.Length - 1) == 0) continue;

            if (buffer.SequenceEqual(aob.Skip(1)))
            {
                yield return position;
            }
        }
    }

    public static IEnumerable<long> ScanAOB(this Stream stream, params byte?[] aob)
    {
        long position;
        byte[] buffer = new byte[aob.Length - 1];

        while ((position = stream.Position) < stream.Length)
        {
            if (stream.ReadByte() != aob[0]) continue;
            if (stream.Read(buffer, 0, aob.Length - 1) == 0) continue;

            if (buffer.Cast<byte?>().SequenceEqual(aob.Skip(1), new AobComparer()))
            {
                yield return position;
            }
        }
    }

    private class AobComparer : IEqualityComparer<byte?>
    {
        public bool Equals(byte? x, byte? y) => x == null || y == null || x == y;
        public int GetHashCode(byte? obj) => obj?.GetHashCode() ?? 0;
    }
}
公共静态类StreamExtensions
{
公共静态IEnumerable ScanAOB(此流,参数字节[]aob)
{
多头仓位;
字节[]缓冲区=新字节[aob.Length-1];
而((位置=流位置)<流长度)
{
如果(stream.ReadByte()!=aob[0])继续;
如果(stream.Read(buffer,0,aob.Length-1)==0)继续;
if(buffer.SequenceEqual(aob.Skip(1)))
{
收益返回位置;
}
}
}
公共静态IEnumerable ScanAOB(此流,参数字节?[]aob)
{
多头仓位;
字节[]缓冲区=新字节[aob.Length-1];
而((位置=流位置)<流长度)
{
如果(stream.ReadByte()!=aob[0])继续;
如果(stream.Read(buffer,0,aob.Length-1)==0)继续;
if(buffer.Cast().SequenceEqual(aob.Skip(1),new AobComparer())
{
收益返回位置;
}
}
}
私有类AOB比较程序:IEqualityComparer
{
公共布尔等于(字节?x,字节?y)=>x==null | | y==null | | x==y;
public int GetHashCode(字节?obj)=>obj?.GetHashCode()??0;
}
}
例如:

void Main()
{
    using (var stream = new MemoryStream(FakeData().ToArray()))
    {
        stream.ScanAOB(0x1, 0x2).Dump("Addresses of: 01 02");
        stream.Position = 0;
        stream.ScanAOB(0x03, 0x12).Dump("Addresses of: 03 12");
        stream.Position = 0;
        stream.ScanAOB(0x04, null, 0x06).Dump("Addresses of: 04 ?? 06");
    }
}

// Define other methods and classes here
IEnumerable<byte> FakeData()
{
    return Enumerable.Range(0, 2)
        .SelectMany(_ => Enumerable.Range(0, 255))
        .Select(x => (byte)x);
}
void Main()
{
使用(var stream=newmemoryStream(FakeData().ToArray()))
{
stream.ScanAOB(0x1,0x2).Dump(“地址:01 02”);
流位置=0;
stream.ScanAOB(0x03,0x12).Dump(“地址:0312”);
流位置=0;
stream.ScanAOB(0x04,null,0x06).Dump(“地址:04×06”);
}
}
//在此处定义其他方法和类
IEnumerable FakeData()
{
返回可枚举的范围(0,2)
.SelectMany(=>Enumerable.Range(0255))
.选择(x=>(字节)x);
}

你能举个例子吗?你需要找到连续的字节吗?或者它们可以在任何地方/彼此不相邻吗?我基本上想在二进制中搜索唯一的字节,然后修补它。因此,在这个二进制中唯一的字节的例子是:“93 0E 40 F9 53 00 B5 DE”(HxD中的二进制)好的,那么为什么你不能找到起始目标字节的位置,并使用
ReadBytes()
来获取块?如果你正在进行替换,你应该已经知道大小了。我刚刚阅读了BinaryReader.ReadBytes()文档(不知道这个文档也存在),但是我真的不明白他们为什么在示例中使用数组&我真的不明白文档如何将其应用于我自己。搜索数组的任何字节都会多次出现在其中吗?只是尝试了一下。但是,它给我的偏移量与我搜索的字节不匹配。此外,BaseStream.Write给了我以下错误:“System.IndexOutOfRangeException:'索引超出了数组的界限。”"很抱歉造成混淆,我做了一些更改,因为我认为您不希望只匹配起始字节,对吗?它可能出现在文件中的任何位置,如果它不是完全匹配的话,这将导致一些损坏?是的,我需要它是匹配的整个字节。我刚刚尝试了更新的代码。它给了我偏移量1302269&从字节22开始。因此,不确定出了什么问题。很难说,因为我不熟悉您的二进制文件或您在文件中搜索的内容的细节。我修改了您问题中的这一行,以匹配您提供的序列,这可能解释了为什么它与您开始的不匹配…
br.BaseStream.ReadByte()=(byte)0x93
您最初是在寻找
0xC0
,但提供了此示例。
93 0E 40 F9 53 00 B5 DE
这就是为什么我发表评论说您需要修改搜索条件以满足您的需要。哇!这看起来很棒!我认为它可以使用,谢谢!