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 }