Reputation: 67345
I have a Unicode string and I need to know the number of bytes it uses.
In general, I know that wcslen(s) * 2
will work. But my understanding is that this is not reliable when working with Unicode.
Is there a reliable and performant way to get the number of bytes used by a Unicode string?
Upvotes: 1
Views: 1034
Reputation: 51511
wcslen counts the number of wchar_t
entities, until it finds a NUL character. It doesn't interpret the data in any way.
(wcslen(s) + 1) * sizeof(wchar_t)
will always, reliably calculate the number of bytes required to store the string s
.
Upvotes: 3