`

strncmp()的陷进

 
阅读更多

最近在做一个报文通信的事情,需要对报文的帧数据进行比较,帧头有固定的格式,比如:“0xbf, 0x13, 0x97, 0x74 ....”,所以需要对接收到的数据进行帧头的比较,我想到了用strncmp()这个C库里的函数,对相关字节进行比对,于是我就如下写了:

 

#include <stdio.h>
#include <string.h>

int main()
{
	char pushbuf[] = {0xbf, 0x13, 0x97,0x74, 0x00,  0x00, 0x40, 0x04, 0x00, 0x00, 0x00, 0xfc, 0xbf};
	char buffer[] = {0xbf, 0x13, 0x97,0x74, 0x00,  0x77, 0x8, 0x0, 0x0, 0x0, 0x2b, 0x45, 0x6d, 0x38, 0xe9, 0xa};

	if(strncmp(buffer, pushbuf, 14) == 0)
	{
		puts("竟然相等!!!\n");
	}
	else
	{
		puts("应该这样才对\n");
	}
	int i = 0;
	while(i<14)
	{
		printf("%2x---------%2x\n", pushbuf[i], buffer[i]);
		i++;
	}
}

 早在第6位就已经出现数据不等了,可是程序运行结果竟然是:

竟然相等!!!

经过长达1个小时的苦苦测试,发现这是strncmp中的一个陷进:

strncmp(char *str1, char *str2, int maxlen)函数是比较两个字符串前maxlen字符是否相等,如果等返回0,不等返回第一个不等字符的差(str1-str2)

但是,它一旦两个字符串在maxlen之前同时遇到了'\0'结束符,就结束比对了,而我的比较就是这么悲剧得相等了!

 

解决这个问题的办法是,用memcmp()函数 :

百度百科 写道
原型:int memcmp(const void *buf1, const void *buf2, unsigned int count);
用法:#include <string.h>或#include<memory.h>
功能:比较内存区域buf1和buf2的前count个字节。
说明:
当buf1<buf2时,返回值<0
当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0

 

 OK!

 

分享到:
评论

相关推荐

    strncat strncpy strncmp memcpy memcmp 比较及其原型

    strcmp strcpy strcat strlen 的实现 以及与strncat strncpy strncmp memcpy memcmp 原型分析

    day7_strncmp.c

    day7_strncmp.c

    PHP中strncmp()函数比较两个字符串前2个字符是否相等的方法

    strncmp()函数定义如下: strncmp(string1,string2,length) 参数说明: string1 必需。规定要比较的首个字符串。 string2 必需。规定要比较的第二个字符串。 length 必需。规定比较中所用的每个字符串的字符数。 ...

    nl_strncmp.rar_The Chain

    The mbuf chain containing the packet will be freed. The mbuf opt, if present, will not be freed.

    需求文档——需求文档怎么写

    一、要讨论怎么写需求文档,首先就的搞清楚需求的构成,我是这么分的: 1、功能需求; 2、非功能需求或技术需求; 我一般把功能需求划分为几个部分: a、业务过程; b、业务规则; c、业务数据;

    strncmpr:比较字符串或单元格字符串的最后 N 个字符(快速 C-Mex)-matlab开发

    输入和输出等于Matlab的STRNCMP/STRNCMPI,但字符串是从右到左比较的。 T = strncmpr(S1, S2, N) 或 T = strncmpir(S1, S2, N) 输入: S1、S2:字符串或单元格字符串。 N:要比较的字符数。 输出: T:逻辑数组,...

    字符串比较

    字符串比较函数 strcmp,strncmp 源码字符串比较函数 strcmp,strncmp 源码字符串比较函数 strcmp,strncmp 源码

    android 电阻触摸屏校验源码及分析 for android 2.1

    最近做了个android 电阻式触摸屏的校验,分析请参考我blog的文章。 少上传了property_service.c 只修改... if(strncmp("ts.config.calibrate",name,strlen("ts.config.calibrate"))==0) { return 1; } return 0; }

    宽字符处理函数函数与普通函数对照表

    wcsncmp() strncmp() 等

    C++ SMTP类发送邮件

    if(strncmp(m_recvbuff,"250",3)!=NULL) { m_error=MAIL_FROM_ERROR; return 0; } //发送rcpt to 返回250 sprintf(m_sendbuff,"%s%s%s","rcpt to:,m_rcptto,"&gt;\r\n"); if(senddata()==0) { return 0; } ...

    编写如按标准字符串操作函数

    :他们最多对参数字符串中的前n个字符进行操作。例如,函数strncpy(s, t, n)将t中最多前n个字符复制到s中。 (注:通过使用命令行参数... (2) void strncmp(s,t,n); 2 (3) int strlen(s); 3 (4) int strncat(s,t,n); 4

    Keil C51 库函数源码

    声 明: 逆向以学习和研究为目的,版权属于Keil公司. ... keil c51的库函数一部分是c语言写的,一部分是汇编写的,c语言写的全部逆向完成,绝大部分生成...strncmp strncpy strpbrk strrchr strrpbrk strrpos strspn strstr

    C语言函数库函数详细介绍手册

    strncmp strncmpi strncpy strnicmp strpbrk strrev strset strstr strtok strupr 数学函数 abs acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp hypot ldexp log log...

    C 语言库函数使用手册

    strncmp strncmpi strncpy strnicmp strpbrk strrev strset strstr strtok strupr 数学函数 abs acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp hypot ldexp log log...

    C语言库函数速查 CHM

    strncmp strncmpi strncpy strnicmp strpbrk strrev strset strstr strtok strupr 数学函数 abs acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp hypot ldexp log log10...

    C语言函数速查手册

    strncmp strncmpi strncpy strnicmp strpbrk strrev strset strstr strtok strupr 数学函数 abs acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp hypot ldexp log log...

    C语言函数速查

    strncmp strncmpi strncpy strnicmp strpbrk strrev strset strstr strtok strupr 数学函数 abs acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp hypot ldexp log log...

    C语言checklist

    Strcpy(),strcat(),strcmp(),strncpy(),strncat(),strncmp()内部到底是如何运行的?这些函数到底对源字符串和目标字符串做了些什么?你是否观察过它们运行时两个字符串内存的变化? 上面这些函数使用时,各有哪些...

    PHP中strcmp()和strcasecmp()函数字符串比较用法分析

    分享给大家供大家参考,...PS:strcmp()函数与 strncmp() 函数类似,不同的是,strncmp()可以指定每个字符串用于比较的字符数。 二、PHP中strcasecmp()函数比较两个字符串(不区分大小写),其定义如下: strcasecmp(st

Global site tag (gtag.js) - Google Analytics