Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I get the byte size of wstring?

196 views
Skip to first unread message

Water Lin

unread,
Aug 26, 2009, 5:34:24 AM8/26/09
to

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.....

--
Water Lin's blog: http://blog.waterlin.org

Ron

unread,
Aug 26, 2009, 7:27:16 AM8/26/09
to
On Aug 26, 5:34 am, Water Lin <Water...@ymail.com> wrote:

>
> 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);

Paavo Helde

unread,
Aug 28, 2009, 6:09:20 PM8/28/09
to
Water Lin <Wate...@ymail.com> kirjutas:

>
> 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

Duke Robillard

unread,
Oct 14, 2009, 2:07:59 PM10/14/09
to

On Fri, 28 Aug 2009 17:09:20 -0500, Paavo Helde wrote:
> in many implementations (Linux, Mac,...) the size of wchar_t is 4
> (assuming UCS-32 encoding instead).

typo check:

s/UCS-32/UTF-32

or perhaps

s/UCS-32/UCS-4

Duke

0 new messages