C# 二进制字节流查找函数IndexOf

        /// <summary>
/// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引。
/// </summary>
/// <param name="srcBytes">被运行查找的 System.Byte[]。</param>
/// <param name="searchBytes">要查找的 System.Byte[]。</param>
/// <returns>假设找到该字节数组。则为 searchBytes 的索引位置。假设未找到该字节数组。则为 -1。假设 searchBytes 为 null 或者长度为0。则返回值为 -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] == searchBytes[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;
}

使用演示样例:

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;
}

C# 二进制字节流查找函数IndexOf的更多相关文章

  1. Java字节流read函数

    问题引入 做Java作业从标准输入流获取用户输入,用到了System.in.read(),然后出现了bug. //随机生成一个小写字母,用户猜5次,读取用户输入,并判断是否猜对 import java ...

  2. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  3. php 将16进制数串转换为二进制数据的函数

    /**     * 将16进制数串转换为二进制数据的函数     * @param $hexdata     * @return string bindata     */     function ...

  4. Excel Vlookup 列查找函数

    列查找函数语法:vlookup(lookup_value,table_array,col_index_num,[range_lookup]) lookup_value:要查找的值,数值.引用或文本字符 ...

  5. C/C++字符串查找函数

    C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...

  6. C/C++字符串查找函数 <转>

    C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...

  7. (查找函数+atoi)判断与(注册函数+strcmp函数)判断两种方法

    loadrunner中接口判断的2中方法    如下: 1. ●查找函数web_reg_find() ● atoi():将字符串转换为整型值 作比较  > 0 Action() { //检查点函 ...

  8. sublime快捷键:快速查找函数和快速匹配括号

    1. 快速查找函数 Ctrl+R 2. 快速匹配括号 光标置于括号中,Ctrl+Shift+M 快速匹配括号内容,再按下 Ctrl+Shift+[ 折叠代码, Ctrl+Shift+] 展开代码. 3 ...

  9. C++ string的查找函数和npos特殊值

    STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找. 3.find_first_of() 4.find_not_first_of() 5.find_ ...

随机推荐

  1. SmartGit STUDY 2

    The Index The Index is an intermediate cache for preparing a commit. With SmartGit, you can make hea ...

  2. 8、NFC技术:让Android自动打开网页

    创建封装Uri的NdefRecord  public  NdefRecord  createUri(String  uriString);  public  NdefRecord  cre ...

  3. Selenium启动本地firefox的profile

    ProfilesIni pi = new ProfilesIni();FirefoxProfile profile = pi.getProfile("default");WebDr ...

  4. 高手就用Chrome不安全模式

    背景:最近玩CSS3和HTML玩得不可开交. 既然要用浏览器的话,就最好在浏览器中设置一个主页,以前徒简洁就一直用百度的搜索页做主页,但是现在百度邪恶的各种广告实在让我恶心,而且一些文献资料中国网站上 ...

  5. Java 分割文件 注意事项

    public static void main(String args[]) throws Exception { if (args.length < 1) { System.exit(0); ...

  6. 学习内容:Html5+Axure原型设计

    今日主要在http://www.runoob.com/html/html5-intro.html和http://www.imooc.com/learn/9网站上学习Html的知识,head.title ...

  7. 【转】What's the difference between simulation and emulation

    摘要:这2个单词 还是用英文解释,比较准确.按我的理解:simulation就是模拟,可以做些改变. emulation是仿真,是按照原来的样子进行部署,不可以改变. Yes, the concept ...

  8. 【转】 Linux Shell 命令--rename

    重命名文件,经常用到mv命令,批量重命名文件rename是最好的选择,Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,判断方法:输入man rename 看到第 ...

  9. iOS 后台退出app时不执行applicationWillTerminate的临时解决方法

    - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release s ...

  10. 关于 python 的 @property总结和思考

    其实关于@property我到处去搜了很多教程来看,因为公司大量使用了oop的编程而我以前很少写,所以现在来重新补过来. 从使用上来说 加了@property之后最明显的区别就是 class Stud ...