Getting the wchar_t string length in C++

thebluetropics 1,046 Reputation points
2022-10-05T06:37:42.19+00:00

Win32 uses UTF-16 encoding for wchar_t to store strings.
Each character can eat from 1 wchar_t to 2 wchar_t(s), depending on the character.

However, either wcslenW() and lstrlenW() does not count character with 2 wchar_t's as single character.

This Japanese Kanji uses only 1 code unit (1 wchar_t):

   wchar_t text[] = L"私";  
   int length = lstrlenW(text); // Outputs 1, as expected  

However, when I use Chinese Character U+2070E, that uses 2 code units (2 wchar_t's), it counts as two character instead of 1 character.
I can't put the code here for some reason, here is the link to the code.

So, I assume that lstrlenW() and wcslen() is counting the total of wchar_t, not the total of characters :I

Is there a way to get the correct length of wchar_t in Win32 applications?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,448 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,562 questions
{count} votes

0 additional answers

Sort by: Most helpful