C # Replace the first byte array with binary

Source: Internet
Author: User

The byte [] byte array replacement operation is often required for communication-related data operations, such

Several replacement methods are provided.

////// Binary data operation ///Public class HexUtility {////// Binary replacement. If no replacement is made, a duplicate of the original array object is returned .//////Source data///Data to be replaced///Data to be replacedPublic static byte [] Replace (byte [] sourceByteArray, byte [] oldValue, byte [] newValue) {// create new data with 1 byte more int newArrayLen = (int) (newValue. length/(double) oldValue. length) * sourceByteArray. length) + 1; // obtain the array Length newArrayLen = Math. max (newArrayLen, sourceByteArray. length); // The new final result array byte [] newByteArray = new byte [newArrayLen]; // The current index of the new array int curIndex = 0; // start and end int start =-1; int end =-1; // The currently searched Index int oldCurindex = 0; // replace data with for (int x = 0; x <sourceByteArray. length; x ++) {// search for 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 ++;} // if (start! =-1 & end! =-1) {// copy and replace the data Buffer. blockCopy (newValue, 0, newByteArray, curIndex, newValue. length); // calculate the offset of the new array. curIndex + = newValue. length; // reset the index start = end =-1 ;}/// process the returned 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 ;}////// Binary replacement. If no replacement is made, a duplicate of the original array object is returned .//////Source data///Data set to be replacedPublic static byte [] Replace (byte [] sourceByteArray, List
 
  
Replaces) {// create new data with 1 byte more int newArrayLen = (int) (replaces. sum (p => p. newValue. length)/(double) replaces. sum (p => p. oldValue. length) * sourceByteArray. length) + 1; // obtain the array Length newArrayLen = Math. max (newArrayLen, sourceByteArray. length); // The new final result array byte [] newByteArray = new byte [newArrayLen]; // The current index of the new array int curIndex = 0; bool find = false; // replace data with for (int x = 0; x <sourceByteArray. length; x ++) {f Oreach (HexReplaceEntity rep in replaces) {// find the data to be replaced 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) {// copy and replace the data Buffer. blockCopy (rep. newValue, 0, newByteArray, curIndex, rep. newValue. length); // calculate the offset of the new array. curIndex + = rep. newValue. length;} else // a problem occurs when the large byte is replaced with the small byte {curIndex-= rep. end-rep. start; // copy and replace the data Buffer. blockCopy (rep. newValue, 0, newByteArray, curIndex, rep. newValue. length); // calculate the offset of the new array. curIndex + = rep. newValue. length ;} // Reset the index rep. start = rep. end =-1; break;} if (! Find) {curIndex ++ ;}/// process the returned 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 ;}}///
  /// Replace the data entity ///Public class HexReplaceEntity {///
  /// Original value to be replaced ///Public byte [] oldValue {get; set ;}///
  /// New value ///Public byte [] newValue {get; set ;}///
  /// Start and end mark by default ///Internal int start =-1 ;///
  /// Start and end mark by default ///Internal int end =-1; // The currently searched index internal int oldCurindex = 0 ;}
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.