A byte[] byte array substitution operation is often required for communication-related data operations. For example, the covariance transformation-related
Several alternative methods are now available
<summary>///Binary data Operations///</summary> public class Hexutility {//<summary> Binary substitution, assuming that no substitution is made to return the copy of the original array pair. </summary>//<param name= "Sourcebytearray" > Source data </param>//<param name= "OldValue" > Required data to be replaced </param>//<param name= "NewValue" > Data to be replaced </param> public static byte[] Repl Ace (byte[] Sourcebytearray, byte[] oldValue, byte[] newvalue) {//Create new data extra 1 bytes int Newarraylen = (int) ((Newvalue.length/(double) oldvalue.length) * sourcebytearray.length) + 1; Get the array length Newarraylen = Math.max (Newarraylen, sourcebytearray.length); New final result array byte[] Newbytearray = new Byte[newarraylen]; The current index of the new array int curindex = 0; Start end int start =-1; int end =-1; The currently found index int oldcurindex = 0; Replace data replace for (intx = 0; x < sourcebytearray.length; X + +) {//Find the data to be replaced 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++; }//Data find complete if (start! =-1 && End! =-1) {//Copy replacement number According to Buffer.blockcopy (newvalue, 0, Newbytearray, Curindex, newvalue.length); Calculates the offset of a new arrayShift amount Curindex + = newvalue.length; Again set index to replicate index start = end =-1; }}//Process return result 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; }///<summary>///binary substitution, assuming that no substitution returns a copy of the original array pair image. </summary>//<param name= "Sourcebytearray" > Source data </param>//<param name= "replaces" > data collection required for replacement </param> public static byte[] Replace (byte[] Sourcebytearray, list
C # Binary replaces the first bullet byte array substitution