对WideCharToMultiByte返回值的迷惑

zjfhgdx 2011-07-15 07:47:01
我的代码:
int iLen2=0;
iLen=WideCharToMultiByte(CP_ACP,0,data.GetBuffer(),data.GetLength(),0,0,0,0);

这里返回的ilen有没有包含结束符啊?我测试了一下,貌似没有包含呀
...全文
253 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
RLib 2011-07-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zjfhgdx 的回复:]
谢谢楼上的,不过你没看后面半句The number includes the byte for the terminating null character.
[/Quote]


没有那句
Waistcoat22 2011-07-15
  • 打赏
  • 举报
回复
你在哪里看的MSDN,

微软最新的:
Return Value

Returns the number of bytes written to the buffer pointed to by lpMultiByteStr if successful. If the function succeeds and cbMultiByte is 0, the return value is the required size, in bytes, for the buffer indicated by lpMultiByteStr.

The function returns 0 if it does not succeed. To get extended error information, the application can call GetLastError, which can return one of the following error codes:

ERROR_INSUFFICIENT_BUFFER. A supplied buffer size was not large enough, or it was incorrectly set to NULL.
ERROR_INVALID_FLAGS. The values supplied for flags were not valid.
ERROR_INVALID_PARAMETER. Any of the parameter values was invalid.
ERROR_NO_UNICODE_TRANSLATION. Invalid Unicode was found in a string.
zjfhgdx 2011-07-15
  • 打赏
  • 举报
回复
谢谢楼上的,不过你没看后面半句The number includes the byte for the terminating null character.
RLib 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 rrrfff 的回复:]
你好呀,我查过了这个应该是不包含'\0'的。

Returns the number of bytes written to the buffer pointed to by lpMultiByteStr if successful.

再说+1没坏处,最好是+2
[/Quote]

http://msdn.microsoft.com/en-us/library/dd374130(VS.85).aspx
RLib 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zjfhgdx 的回复:]
那我在new的时候是不是应该char* buf=new char[ilen+1]啊?我在程序里没有+1,貌似也是正常的。
迷糊迷糊~~
[/Quote]

new出来的东西溢出一两个字节很难发现的
RLib 2011-07-15
  • 打赏
  • 举报
回复
你好呀,我查过了这个应该是不包含'\0'的。

Returns the number of bytes written to the buffer pointed to by lpMultiByteStr if successful.

再说+1没坏处,最好是+2
zjfhgdx 2011-07-15
  • 打赏
  • 举报
回复
+1是没错,但我现在是对这个函数的返回值有点迷惑,能不能给个准确的?难道这个函数有不确定性?
用户 昵称 2011-07-15
  • 打赏
  • 举报
回复
+1没坏处。
zjfhgdx 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 visualeleven 的回复:]
引用 3 楼 zjfhgdx 的回复:

那我在new的时候是不是应该char* buf=new char[ilen+1]啊?我在程序里没有+1,貌似也是正常的。
迷糊迷糊~~

Returns the number of bytes written to the buffer pointed to by lpMultiByteStr if successful. The number ……
[/Quote]

这个我看过了但是
我cstring里的内容是_T("123"),它返回的值是3,这个就让我很迷惑。如果包含空字符,那应该是4才对吧
Waistcoat22 2011-07-15
  • 打赏
  • 举报
回复
其实不用这么纠结,统统+1不会有问题。
Eleven 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zjfhgdx 的回复:]

那我在new的时候是不是应该char* buf=new char[ilen+1]啊?我在程序里没有+1,貌似也是正常的。
迷糊迷糊~~
[/Quote]
Returns the number of bytes written to the buffer pointed to by lpMultiByteStr if successful. The number includes the byte for the terminating null character.
这里说的很清楚了,返回值里包含了空字符,new的时候不用+1
zjfhgdx 2011-07-15
  • 打赏
  • 举报
回复
那我在new的时候是不是应该char* buf=new char[ilen+1]啊?我在程序里没有+1,貌似也是正常的。
迷糊迷糊~~
oyljerry 2011-07-15
  • 打赏
  • 举报
回复
本来就是返回字符串长度,不会包含结束符.
用户 昵称 2011-07-15
  • 打赏
  • 举报
回复
data是什么?结束符是指的0?

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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