返回值
mblen()函数返回从str开始的多字节序列传递的字节数(如果识别出非null宽字符)。如果识别出一个空宽字符,则返回0。如果遇到无效的多字节序列或无法解析完整的多字节字符,则返回-1。
示例
以下示例显示mblen函数的用法-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int len;
char *pmbnull = NULL;
char *pmb = (char *)malloc( MB_CUR_MAX );
wchar_t *pwc = L"Hi";
wchar_t *pwcs = (wchar_t *)malloc( sizeof( wchar_t ));
printf("Converting to multibyte string\n");
len = wcstombs( pmb, pwc, MB_CUR_MAX);
printf("Characters converted %d\n", len);
printf("Hex value of first multibyte character: %#.4x\n", pmb);
len = mblen( pmb, MB_CUR_MAX );
printf( "Length in bytes of multibyte character %x: %u\n", pmb, len );
pmb = NULL;
len = mblen( pmb, MB_CUR_MAX );
printf( "Length in bytes of multibyte character %x: %u\n", pmb, len );
return(0);
}
尝试一下
让我们编译并运行上面的程序,它将产生以下结果-
Converting to multibyte string
Characters converted 1
Hex value of first multibyte character: 0x168c6010
Length in bytes of multibyte character 168c6010: 1
Length in bytes of multibyte character 0: 0