首页 / C# / C#替换二进制文件中的十六进制
C#替换二进制文件中的十六进制
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#替换二进制文件中的十六进制,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2181字,纯文字阅读大概需要4分钟。
内容图文
![C#替换二进制文件中的十六进制](/upload/InfoBanner/zyjiaocheng/672/7026acea8abe4d9e8b3ef148f3adc8dc.jpg)
我有一个二进制文件,应该更改几个值.
更确切地说,在文件的两个部分的开头,有两个十六进制值
66 73 69 6D 35 2E 36 39
应该更改为
4D 53 57 49 4E 34 2E 31
我如何才能做到这一点,并尽快?我已经到了将整个文件读入byte []数组的地步,但是此类没有搜索或替换功能.
解决方法:
这是我写的一种方法,可用于查找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;
}
内容总结
以上是互联网集市为您收集整理的C#替换二进制文件中的十六进制全部内容,希望文章能够帮你解决C#替换二进制文件中的十六进制所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。