Jonathan Wood
Jonathan Wood

Reputation: 67345

Getting the number of bytes in a Unicode string

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

Answers (1)

IInspectable
IInspectable

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

Related Questions