积极答复者
StrCmpN BUG?

问题
-
#include "stdafx.h"
#include <stdio.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")int main(int argc, char* argv[])
{
int id1=StrCmpN ("B","c",1);
int id2=StrCmpN ("a","A",1);
int id3=StrCmpN ("c","A",1);
printf("%d,%d,%d \n",id1,id2,id3);
return 0;
}output:
-1,-1,1
Press any key to continueXP SP3 VC6/VB6/VS2008
答案
-
你好,
经过我的再次的实验,我也得到了你所得到的结果。我也发现了StrCmpN的排序方式不是按照ASC码来排序的。它是通过字母表顺序来排序的。
例如:StrCmpN ("B","c",1); 正如字母表中的顺序B是在C之后因此,得到的结果是B<C,同理,StrCmpN ("c","A",1); C也大于A。 如果是相同字母在比较是,大写字母A是大于小写字母a。因此StrCmpN ("a","A",1) 得到-1的结果。
希望我这次的回答能够解决你的疑问。
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Rob Pan 2011年5月4日 8:22
全部回复
-
你好,
StrCmpN(PCTSTR psz1,PCTSTR psz2,int nChar)是判断两个字符串的大小,当字符串相等是返回值是0,当psz1大于psz2时,返回一个正数,当psz1小于psz2时返回一个负数。
我对你的程序做了简单的修改,帮助你解决你的问题。
int BB = 'B';
int CC = 'c';
int AA = 'A';
int a = 'a';
printf(" BB(B): %d \n CC(c): %d \n AA(A): %d \n a(a): %d",BB,CC,AA,a);
int id1=StrCmpN ("B","c",1);
int id2=StrCmpN ("a","A",1);
int id3=StrCmpN ("c","A",1);
printf("%d,%d,%d \n",id1,id2,id3);
return 0;
你可以得到这样的结果: BB(B): 66
CC(c): 99
AA(A): 65
a(a): 97
因为StrCmpN在判断单个字符的时候会装换成字符对应的ASC码,因此会出现你所得到的结果。
希望我的回答对你的疑问有所帮助
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
你好,
经过我的再次的实验,我也得到了你所得到的结果。我也发现了StrCmpN的排序方式不是按照ASC码来排序的。它是通过字母表顺序来排序的。
例如:StrCmpN ("B","c",1); 正如字母表中的顺序B是在C之后因此,得到的结果是B<C,同理,StrCmpN ("c","A",1); C也大于A。 如果是相同字母在比较是,大写字母A是大于小写字母a。因此StrCmpN ("a","A",1) 得到-1的结果。
希望我这次的回答能够解决你的疑问。
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Rob Pan 2011年5月4日 8:22