在做通訊相關的數據操作時經常需要用到 byte[] byte 數組替換操作.比如協義轉換相關的
現在提供了幾種替換的方法
////// 二進制數據 操作 /// public class HexUtility { ////// 二進制替換,如果沒有替換則返回原數組對像的復本. /// /// 源數據 /// 需要替換的數據 /// 將要替換成為的數據 public static byte[] Replace(byte[] sourceByteArray, byte[] oldValue, byte[] newValue) { //創建新數據多出1字節 int newArrayLen = (int)((newValue.Length / (double)oldValue.Length) * sourceByteArray.Length) + 1; //得到數組長度 newArrayLen = Math.Max(newArrayLen, sourceByteArray.Length); //新的最後結果數組 byte[] newByteArray = new byte[newArrayLen]; //新數組的當前索引 int curIndex = 0; //開始結束 int start = -1; int end = -1; //當前查找到的索引 int oldCurindex = 0; //替換數據替換 for (int x = 0; x < sourceByteArray.Length; x++) { //查找要替換的數據 if (sourceByteArray[x] == oldValue[oldCurindex]) { if (oldCurindex == 0) { start = x; } if (oldCurindex == oldValue.Length - 1) { end = x; oldCurindex = 0; } else { oldCurindex++; } } else { oldCurindex = 0; newByteArray[curIndex] = sourceByteArray[x]; curIndex++; } //數據查找完成 if (start != -1 && end != -1) { //復制替換數據 Buffer.BlockCopy(newValue, 0, newByteArray, curIndex, newValue.Length); //計算新數組的偏移量 curIndex += newValue.Length; //重新設置需要復制索引的索引 start = end = -1; } } //處理返回結果 byte[] result = null; if (curIndex != 0) { result = new byte[curIndex]; Buffer.BlockCopy(newByteArray, 0, result, 0, result.Length); } else { result = new byte[sourceByteArray.Length]; Buffer.BlockCopy(sourceByteArray, 0, result, 0, result.Length); } return result; } ////// 二進制替換,如果沒有替換則返回原數組對像的復本. /// /// 源數據 /// 需要替換的數據集合 public static byte[] Replace(byte[] sourceByteArray, Listreplaces) { //創建新數據多出1字節 int newArrayLen = (int)((replaces.Sum(p => p.newValue.Length) / (double)replaces.Sum(p => p.oldValue.Length)) * sourceByteArray.Length) + 1; //得到數組長度 newArrayLen = Math.Max(newArrayLen, sourceByteArray.Length); //新的最後結果數組 byte[] newByteArray = new byte[newArrayLen]; //新數組的當前索引 int curIndex = 0; bool find = false; //替換數據替換 for (int x = 0; x < sourceByteArray.Length; x++) { foreach (HexReplaceEntity rep in replaces) { //查找要替換的數據 if (sourceByteArray[x] == rep.oldValue[rep.oldCurindex]) { if (rep.oldCurindex == 0) { rep.start = x; } if (rep.oldCurindex == rep.oldValue.Length - 1) { rep.end = x; rep.oldCurindex = 0; } else { rep.oldCurindex++; } } else { rep.oldCurindex = 0; newByteArray[curIndex] = sourceByteArray[x]; find = false; } //數據查找完成 if (rep.start != -1 && rep.end != -1) { find = true; if (rep.newValue.Length >= rep.oldValue.Length) { //復制替換數據 Buffer.BlockCopy(rep.newValue, 0, newByteArray, curIndex, rep.newValue.Length); //計算新數組的偏移量 curIndex += rep.newValue.Length; } else //由大字節替換為少字節時出現了問題 { curIndex -= rep.end - rep.start; //復制替換數據 Buffer.BlockCopy(rep.newValue, 0, newByteArray, curIndex, rep.newValue.Length); //計算新數組的偏移量 curIndex += rep.newValue.Length; } //重新設置需要復制索引的索引 rep.start = rep.end = -1; break; } } if (!find) { curIndex++; } } //處理返回結果 byte[] result = null; if (curIndex != 0) { result = new byte[curIndex]; Buffer.BlockCopy(newByteArray, 0, result, 0, result.Length); } else { result = new byte[sourceByteArray.Length]; Buffer.BlockCopy(sourceByteArray, 0, result, 0, result.Length); } return result; } } /// /// 替換數據實體 /// public class HexReplaceEntity { ////// 需要替換的原始值 /// public byte[] oldValue { get; set; } ////// 新值 /// public byte[] newValue { get; set; } ////// 默認開始結束標記 /// internal int start = -1; ////// 默認開始結束標記 /// internal int end = -1; //當前查找到的索引 internal int oldCurindex = 0; }