isdigit的问题

xstong1982 2013-01-23 03:30:47
winxp下VC6.0环境,使用isdigit时发现,如果传入的参数大于128,如0xD6,那么isdigit会将其判断成数字,此现象仅在Release时发生,Debug正常。编译选项为_MBCS。在win7下编译则正常。另外换成iswdigit也正常。
求解释。
...全文
286 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-01-23
  • 打赏
  • 举报
回复
和楼主已经达到“教学相长”的境界了!
xstong1982 2013-01-23
  • 打赏
  • 举报
回复
原因找到了,isdigit的参数传的不是0xD6,而是-42,isctype方法是没问题的,_pctype[_c] & _DIGIT显然坐标不对,改成传0xD6就没问题了。
xstong1982 2013-01-23
  • 打赏
  • 举报
回复
按照zhao4zhong1的提示,分别调用isctype(_c,_DIGIT)和_pctype[_c] & _DIGIT,发现前者结果正常,后者有问题,为什么Release和Debug会走不同路径还不太清楚。另外我用的是VC6.0,可能和VS2010还不太一样。
赵4老师 2013-01-23
  • 打赏
  • 举报
回复
http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’,再点立即安装 再参考
File: "C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\ctype.h"
   265: #define isdigit(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_DIGIT) : __chvalidchk(_c, _DIGIT))
File: "C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\isctype.c"
   152: extern "C" int __cdecl _isctype (
   153:         int c,
   154:         int mask
   155:         )
   156: {
   157:     if (__locale_changed == 0)
   158:     {
   159:         return __initiallocinfo.pctype[c] & mask;
   160:     }
   161:     else
   162:     {
   163:         return _isctype_l(c, mask, NULL);
   164:     }
   165: }
File: "C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\isctype.c"
   109: extern "C" int __cdecl _isctype_l (
   110:         int c,
   111:         int mask,
   112:         _locale_t plocinfo
   113:         )
   114: {
   115:         int size;
   116:         unsigned short chartype;
   117:         char buffer[3];
   118:         _LocaleUpdate _loc_update(plocinfo);
   119: 
   120:         /* c valid between -1 and 255 */
   121:         if ( c >= -1 && c <= 255 )
   122:             return _loc_update.GetLocaleT()->locinfo->pctype[c] & mask;
   123: 
   124:         if ( _isleadbyte_l(c >> 8 & 0xff, _loc_update.GetLocaleT()) )
   125:         {
   126:             buffer[0] = (c >> 8 & 0xff); /* put lead-byte at start of str */
   127:             buffer[1] = (char)c;
   128:             buffer[2] = 0;
   129:             size = 2;
   130:         } else {
   131:             buffer[0] = (char)c;
   132:             buffer[1] = 0;
   133:             size = 1;
   134:         }
   135: 
   136:         if ( 0 == __crtGetStringTypeA(
   137:                     _loc_update.GetLocaleT(),
   138:                     CT_CTYPE1,
   139:                     buffer,
   140:                     size,
   141:                     &chartype,
   142:                     _loc_update.GetLocaleT()->locinfo->lc_codepage,
   143:                     _loc_update.GetLocaleT()->locinfo->lc_handle[LC_CTYPE],
   144:                     TRUE) )
   145:         {
   146:             return 0;
   147:         }
   148: 
   149:         return (int)(chartype & mask);
   150: }

xstong1982 2013-01-23
  • 打赏
  • 举报
回复
使用iswdigit我能够理解,我更想知道的是为什么在不同的编译环境下isdigit会表现得不一样。
赵4老师 2013-01-23
  • 打赏
  • 举报
回复
对ASCII大于127的字符当然应该改用iswdigit

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧