C#替换二进制文件中的十六进制内容

3

我有一个二进制文件,其中有几个需要更改的值。 更确切地说,在文件的两个部分,即开头处,有两个十六进制值。

66 73 69 6D 35 2E 36 39

需要做哪些更改

4D 53 57 49 4E 34 2E 31

我应该如何异步地并尽可能快地完成这个任务?我已经将整个文件读入一个byte[]数组中,但是这个类没有搜索或替换功能。


1
为什么需要异步执行?你不能在它自己的线程中运行代码吗? - Mark Byers
你为什么想要更改软盘引导记录?你是病毒编写者还是反病毒编写者? :-) - Thomas Mueller
你可能觉得很奇怪,但这是一个塞班项目 :D 实际上我想将FSIM UDA图像转换为可编辑的MSWIN图像,反之亦然。 - fonix232
1个回答

4

以下是我写的一种方法,可以帮助您找到您要查找的字节在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;
}

@fonix232:这在我的代码里。你确定你复制了全部吗? - Timwi
是的,我确信已经将其复制到我的代码中(不知道是否重要,但这是一个Win控制台应用程序,在Main()中复制函数),并且在 if (sourceArray.SubarrayEquals(i, findWhat, 0, findWhat.Length)) 会告诉我数组没有“SubarrayEquals”这样的函数。 - fonix232
1
@fonix232:这些方法是扩展方法。您必须将它们放入public static class中。很抱歉我没有提到。或者,您可以直接将调用更改为if (SubarrayEquals(sourceArray, i, findWhat, 0, findWhat.Length)) - Timwi
@fonix232:你可能正在使用不支持扩展方法的C# 2.0版本。 - Claus Jørgensen
@Claus:那么他肯定会收到一个指向“this”关键字而不是呼叫的错误。 - Timwi
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接