Example code for C # binary byte stream lookup function indexof

Source: Internet
Author: User
C # binary byte stream lookup function indexof

        <summary>///Report specified system.byte[] The index of the first occurrence in this instance. </summary>//<param name= "Srcbytes" > performed find system.byte[]. </param>//<param name= "Searchbytes" > system.byte[to find]. </param>///<returns> If the byte array is found, the index position of searchbytes, or 1 if the byte array is not found. If searchbytes is null or has a length of 0, the return value is-1. </returns> Internal int IndexOf (byte[] srcbytes, byte[] searchbytes) {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 (int i = 0; i < srcbytes.length-searchbytes.length; i++) {if (srcbytes[i] = = Searchbyt                    Es[0]) {if (searchbytes.length = = 1) {return i;}        BOOL flag = TRUE;            for (int j = 1; j < Searchbytes.length; J + +) {if (srcbytes[i + j                            ] = Searchbytes[j]) {flag = false;                        Break                }} if (flag) {return i;}        }} return-1; }

Examples of Use:

Receivedata = new Byte[1024];int Receivelen = socket. ReceiveFrom (Receivedata, ref EP); Receivedata = this. Subbyte (receivedata, 0, Receivelen); if (this. IndexOf (Receivedata, System.Text.Encoding.Unicode.GetBytes ("Exec_exit"))! =-1) {    this.runing = false;    Break }
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.