c# - How to find and replace a large section of bytes in a file? -


i'm looking find large section of bytes within file, remove them , import new large section of bytes starting old ones started.

here's video of manual process i'm trying re-create in c#, might explain little better: https://www.youtube.com/watch?v=_knx8wttcva

i have basic experience c# learning go along, appreciated!

thanks.

refer question: c# replace bytes in byte[]

use following class:

public static class bytepatternutilities {     private static int findbytes(byte[] src, byte[] find)     {         int index = -1;         int matchindex = 0;         // handle complete source array         (int = 0; < src.length; i++)         {             if (src[i] == find[matchindex])             {                 if (matchindex == (find.length - 1))                 {                     index = - matchindex;                     break;                 }                 matchindex++;             }             else             {                 matchindex = 0;             }          }         return index;     }      public static byte[] replacebytes(byte[] src, byte[] search, byte[] repl)     {         byte[] dst = null;         byte[] temp = null;         int index = findbytes(src, search);         while (index >= 0)         {             if (temp == null)                 temp = src;             else                 temp = dst;              dst = new byte[temp.length - search.length + repl.length];              // before found array             buffer.blockcopy(temp, 0, dst, 0, index);             // repl copy             buffer.blockcopy(repl, 0, dst, index, repl.length);             // rest of src array             buffer.blockcopy(                 temp,                 index + search.length,                 dst,                 index + repl.length,                 temp.length - (index + search.length));               index = findbytes(dst, search);         }         return dst;     } } 

usage:

byte[] allbytes = file.readallbytes(@"your source file path"); byte[] oldbytepattern = new byte[]{49, 50}; byte[] newbytepattern = new byte[]{48, 51, 52}; byte[] resultbytes = bytepatternutilities.replacebytes(allbytes, oldbytepattern, newbytepattern); file.writeallbytes(@"your destination file path", resultbytes) 

the problem when file large, require "windowing" function. don't load bytes in memory take space.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -