Replace byte array range - CSharp System

CSharp examples for System:Byte Array

Description

Replace byte array range

Demo Code


using System.Text;
using System.Collections.Generic;
using System;//from  w  w  w  . j a  v  a  2 s. c  o  m

public class Main{
        public static byte[] Replace(this byte[] input, byte[] pattern, byte[] replacement)
        {
            //TODO: CLEAN

            if (pattern.Length == 0)
            {
                return input;
            }

            List<byte> result = new List<byte>();

            int i;

            for (i = 0; i <= input.Length - pattern.Length; i++)
            {
                bool foundMatch = true;
                for (int j = 0; j < pattern.Length; j++)
                {
                    if (input[i + j] != pattern[j])
                    {
                        foundMatch = false;
                        break;
                    }
                }

                if (foundMatch)
                {
                    result.AddRange(replacement);
                    i += pattern.Length - 1;
                }
                else
                {
                    result.Add(input[i]);
                }
            }

            for (; i < input.Length; i++)
            {
                result.Add(input[i]);
            }

            return result.ToArray();
        }
}

Related Tutorials