C# 替换二进制文件中的 HEX

发布于 2024-09-19 19:08:04 字数 230 浏览 4 评论 0原文

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

66 73 69 6D 35 2E 36 39

应该更改为

4D 53 57 49 4E 34 2E 31

如何尽可能快地执行此异步操作?我已经将整个文件读入 byte[] 数组,但该类没有搜索或替换功能。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦行僧 2024-09-26 19:08:04

这是我编写的一个方法,您可以使用它来查找 byte[] 中您要查找的字节。

/// <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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文