C Library Functions -mblen()
C Standard Library - <stdlib.h>
Description
C Library Functionsint mblen(const char *str, size_t n)Return parameterstrThe length of the multibyte character pointed to.
Declaration
Below is the declaration of the mblen() function.
int mblen(const char *str, size_t n)
Parameter
- str-- Pointer to the first byte of a multibyte character.
- n-- Maximum number of bytes to check for the character length.
Return Value
If a non-null wide character is recognized, the mblen() function returns the number of bytes parsed from the multibyte sequence starting at str. If a null wide character is recognized, it returns 0. If an invalid multibyte sequence is recognized, or a complete multibyte character cannot be parsed, it returns -1.
Example
The following example demonstrates the usage of the mblen() function.
#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("转换为多字节字符串\n");
len = wcstombs( pmb, pwc, MB_CUR_MAX);
printf("被转换的字符 %d\n", len);
printf("第一个多字节字符的十六进制值:%#.4x\n", pmb);
len = mblen( pmb, MB_CUR_MAX );
printf( "多字节字符 %x 的字节长度:%u\n", pmb, len );
pmb = NULL;
len = mblen( pmb, MB_CUR_MAX );
printf( "多字节字符 %x 的字节长度:%u\n", pmb, len );
return(0);
}
Let us compile and run the above program, which will produce the following result:
转换为多字节字符串 被转换的字符 1 第一个多字节字符的十六进制值:0x168c6010 多字节字符 168c6010 的字节长度:1 多字节字符 0 的字节长度:0other extensions