Hi,
i have the question on how to determine the
string length of a wide string and a multibyte string:
1. Number of letters (one letter may use three bytes)
2. Number of bytes
In the code snippet *p points to one chinese word which
i copy/pasted from my browser from some chinese homepage,
followed by a german umlaut.
So i would expect '2 letters' but '5 bytes'
In the code comments the results are given.
So how can i determine the number of bytes versus the number
of letters?
Thanks
Marcel
/*
gcc -Wall -pedantic -o Hello Hello.c -std=c99
*/
#include <stdio.h/* printf() */
#include <wchar.h/* wprintf (use -std=c99 compile flag */
#include <locale.h/* setlocal() */
#include <string.h/* strlen() */
#include <stdlib.h/* mbstowcs() mblen() */
int main()
{
setlocale(LC_CTYPE, "en_US");
char *p = "統ö";
size_t len = mblen(p, 100);
printf("multibyte string: '%s' strlen=%d mblen=%d\n", p, (int)strlen(p), (int)len);
/* --multibyte string: '統ö' strlen=5 mblen=1 */
wchar_t pwcs[126];
mbstowcs(pwcs, p, 124);
// wcslen() computes the number of wide characters in the string??
printf("wide string: '%ls' wcslen=%d\n", pwcs, (int)wcslen(pwcs));
/* --wide string: '統ö' wcslen=5 */
return 0;
}