C # binary file operations (Content Search and data truncation)

Source: Internet
Author: User
Private void button2_click (Object sender, eventargs e) {var list = new list <frame> (); byte [] Buf; byte [] bytstart = {0x70, 0x6d, 0x6a, 0x73, 0x7a, 0x6a, 0x66, 0x72, 0x6d}; byte [] bytend = {0x70, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x65, 0x6e, 0x64}; using (var fs = new filestream (@ "D: \ Avi \ 6.lxf", filemode. open) {Buf = new byte [FS. length]; var Len = (INT) FS. length; FS. read (BUF, 0, Len);} var fstart = 0; var fend = 0; var offset = 0; do {fstart = indexof (BUF, bytstart, offset); fend = indexof (BUF, bytend, fstart); debug. print ("{0 }~ {1} ", fstart, fend); If (fstart =-1 | fend =-1) {offset = Buf. length;} else {var frame = new frame {start = fstart + bytstart. length, end = fend,}; frame. length = frame. end-frame. start; frame. data = new byte [frame. length];
// Content capture
Array. copy (BUF, frame. start, frame. data, 0, frame. length); list. add (FRAME); offset = fend; offset + = bytend. length ;}} while (offset <Buf. length );}

  

Method for searching strings

/// <Summary> /// report the index of the first matching item of the specified system. byte [] in this instance. /// </Summary> /// <Param name = "srcbytes"> system. byte [] to be searched. </Param> /// <Param name = "searchbytes"> system. byte [] to be searched. </Param> // <returns> if this byte array is found, the index position of searchbytes is used. If this byte array is not found, the value is-1. If searchbytes is null or the length is 0, the returned value is-1. </Returns> internal int indexof (byte [] srcbytes, byte [] searchbytes, int offset = 0) {If (offset =-1) {return-1 ;} if (srcbytes = NULL) {return-1;} If (searchbytes = NULL) {return-1;} If (srcbytes. length = 0) {return-1;} If (searchbytes. length = 0) {return-1;} If (srcbytes. length <searchbytes. length) {return-1 ;}for (VAR I = offset; I <srcbytes. length-searchbytes. length; I ++) {If (srcbytes [I]! = Searchbytes [0]) continue; If (searchbytes. length = 1) {return I;} var flag = true; For (VAR j = 1; j <searchbytes. length; j ++) {If (srcbytes [I + J]! = Searchbytes [J]) {flag = false; break ;}}if (FLAG) {return I ;}} return-1 ;}

Real-world class

    public class Frame    {        public int Start { get; set; }        public int End { get; set; }        public int Length { get; set; }        public byte[] Data { get; set; }    }

 

C # binary file operations (Content Search and data truncation)

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.