How can I get the byte size of std::wstring?

徘徊边缘 提交于 2019-12-10 01:36:31

问题


I am using std::wstring as my Unicode style string. Now I want to get the byte size of a wstring. If I use size() method of wstring, I just get the total number of chars in my wstring. But the byte should be size() * 2. Is there an official way to get this byte size? I don't want to use size() * 2 in my program.....

I want to use in RegSetValueExW as last parameter.


回答1:


Use str.size() * sizeof(wchar_t) or equivalent.

In fact, I can't think of any other platform besides Windows that has 2-byte wchar_t, 4 bytes is far more common.




回答2:


Why not :

( str.size() * sizeof(wchar_t) ) ;

Or :

( str.size() * sizeof(std::wstring::traits_type::char_type) ) ;



回答3:


std::wstring s = L"Howdy, world!";
//...
std::size_t bytes_count = s.size() * sizeof s[0];



回答4:


You can use wcslen function

MSDN link: http://msdn.microsoft.com/en-us/library/78zh94ax.aspx

Linux: http://linux.die.net/man/3/wcslen



来源:https://stackoverflow.com/questions/9278723/how-can-i-get-the-byte-size-of-stdwstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!