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.....
--
Water Lin's blog: http://blog.waterlin.org
>
> Is there an official way to get this byte size? I don't want to use
> size() * 2 in my program.....
>
size() * sizeof (std::wstring::char_type);
>
> I am using 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.....
>
Your (literary) question is already answered elsethread. However, if you
need to know the byte size of wstring elements, then you most probably
also need to know the encoding what you are using. And the encoding
basically fixes the element size more stringently, thus making your
question meaningless. For example, if your encoding is UTF-16 (as
suggested by your assumption that the element size is 2), then you would
have problems with portably using std::wstring for holding it, because in
many implementations (Linux, Mac,...) the size of wchar_t is 4 (assuming
UCS-32 encoding instead).
Maybe you want something like this instead?
STATIC_ASSERT(sizeof(std::wstring::char_type)==2);
hth
Paavo
typo check:
s/UCS-32/UTF-32
or perhaps
s/UCS-32/UCS-4
Duke