MFC 中的 Unicode strlen 等效项
考虑以下简单的代码:
GetDlgItemText(IDC_EName,LPTSTR(cName),11);
k=strlen(cName);
我想获取用户在编辑框中输入的字符串的长度,但我有 错误的结果 K=1 (总是)因为它是 unicode 字符串并且它得到 第一个字符和第二个字符为空,我不知道如何修复它。 欢迎任何评论。 问候,
Consider the following simple code:
GetDlgItemText(IDC_EName,LPTSTR(cName),11);
k=strlen(cName);
I want to get the length of String that user puts at edit box but I have
the wrong result K=1 (always) because it's unicode string and it get
the first character and the second is null and I donot know how to fix it.
Any comment is welcome.
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
GetDlgItemText()
会返回字符串的长度,或者更准确地说,复制到输出缓冲区的字符数量(不是字节)。您还可以直接检查字符串的长度。由于您使用 ANSI/Unicode 兼容的宏,例如
LPTSTR
,因此您应该使用 ANSI/Unicode 兼容的 strlen 函数:_tcslen(cName)_tcslen()
解析为编译为 ANSI/MBCS 时为strlen()
,编译为 Unicode 时为wcslen()
。The length of the string, or more accurately the amount of characters (not bytes) copied to the output buffer is returned by your call to
GetDlgItemText()
.You can also check the length of the string directly. Since you use ANSI/Unicode compatible macros such as
LPTSTR
, you should use a ANSI/Unicode compatible strlen function: _tcslen(cName)_tcslen()
resolves tostrlen()
when compiling to ANSI/MBCS and towcslen()
when compiling to Unicode.使用
wcslen()
http://msdn.microsoft.com/en-us/library/78zh94ax%28v=vs.80%29.aspx当然你可以这样做:
因为返回value 指定复制到缓冲区的字符数。 http://msdn.microsoft.com/en -us/library/ms645489%28v=vs.85%29.aspx
Use
wcslen()
http://msdn.microsoft.com/en-us/library/78zh94ax%28v=vs.80%29.aspxOf course you could just do:
since the return value specifies the number of characters copied to the buffer. http://msdn.microsoft.com/en-us/library/ms645489%28v=vs.85%29.aspx