C#替换二进制文件HEX [英] C# Replace HEX in binary file

查看:122
本文介绍了C#替换二进制文件HEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件,其中有一些什么样的值应该被改变。
更确切地说,在该文件的两个部分,在开始的时候,有两个十六进制值

I have a binary file, where are a few values what should be changed. To be more exact, at two part of the file, in the beginning, there are two HEX values

66 73 69 6D 35 2E 36 39

什么应该被更改为

what should be changed to

4D 53 57 49 4E 34 2E 31

我怎么能这样做异步,并尽可能快?我已经得到的地步,我看了整个文件转换成byte []数组,但这一类没有搜索或替换功能。

How could I do this async, and as fast as possible? I've got to the point where I read the whole file into a byte[] array, but this class has no search or replace feature.

推荐答案

下面是我写的一个方法,你可以用它来寻找其中的字节[] 字节是,你正在努力寻找。

Here is a method I wrote which you can use to find where in your byte[] the bytes are that you are trying to find.

/// <summary>
/// Searches the current array for a specified subarray and returns the index
/// of the first occurrence, or -1 if not found.
/// </summary>
/// <param name="sourceArray">Array in which to search for the
/// subarray.</param>
/// <param name="findWhat">Subarray to search for.</param>
/// <param name="startIndex">Index in <paramref name="sourceArray"/> at which
/// to start searching.</param>
/// <param name="sourceLength">Maximum length of the source array to search.
/// The greatest index that can be returned is this minus the length of
/// <paramref name="findWhat"/>.</param>
public static int IndexOfSubarray<T>(this T[] sourceArray, T[] findWhat,
        int startIndex, int sourceLength) where T : IEquatable<T>
{
    if (sourceArray == null)
        throw new ArgumentNullException("sourceArray");
    if (findWhat == null)
        throw new ArgumentNullException("findWhat");
    if (startIndex < 0 || startIndex > sourceArray.Length)
        throw new ArgumentOutOfRangeException();
    var maxIndex = sourceLength - findWhat.Length;
    for (int i = startIndex; i <= maxIndex; i++)
    {
        if (sourceArray.SubarrayEquals(i, findWhat, 0, findWhat.Length))
            return i;
    }
    return -1;
}

/// <summary>Determines whether the two arrays contain the same content in the
/// specified location.</summary>
public static bool SubarrayEquals<T>(this T[] sourceArray,
        int sourceStartIndex, T[] otherArray, int otherStartIndex, int length)
        where T : IEquatable<T>
{
    if (sourceArray == null)
        throw new ArgumentNullException("sourceArray");
    if (otherArray == null)
        throw new ArgumentNullException("otherArray");
    if (sourceStartIndex < 0 || length < 0 || otherStartIndex < 0 ||
        sourceStartIndex + length > sourceArray.Length ||
        otherStartIndex + length > otherArray.Length)
        throw new ArgumentOutOfRangeException();

    for (int i = 0; i < length; i++)
    {
        if (!sourceArray[sourceStartIndex + i]
            .Equals(otherArray[otherStartIndex + i]))
            return false;
    }
    return true;
}

这篇关于C#替换二进制文件HEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆