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

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

I want to use in RegSetValueExW as the last parameter.

+8
c ++ string winapi size wstring
source share
4 answers

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

In fact, I can not think of any other platform besides Windows, which has 2 bytes wchar_t , 4 bytes are much more common.

+13
source share

Why not:

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

Or:

 ( str.size() * sizeof(std::wstring::traits_type::char_type) ) ; 
+7
source share
 std::wstring s = L"Howdy, world!"; //... std::size_t bytes_count = s.size() * sizeof s[0]; 
+1
source share
-3
source share

All Articles