Get the length and determine the validity of a multibyte character.
#include <stdlib.h>
int mblen( const char *mbstr, size_t count );
int __far _fmblen(const char __far *mbstr, size_t count );
mbstr | The address of a sequence of bytes (a multibyte character) | |
count | The number of bytes to check |
The mblen function returns the length in bytes of a valid multibyte character. It examines count or fewer bytes contained in mbstr. It will not examine more than MB_CUR_MAX bytes.
The _fmblen function is a model-independent (large-model) form of the mblen function.
If mbstr is not NULL, both mblen and _fmblen return the length, in bytes, of the multibyte character. If mbstr is NULL, or the object that it points to is the wide-character null character (L'\0'), both functions return 0. If the object that mbstr points to does not form a valid multibyte character within the first count characters, both functions return –1.
mblen
Standards:ANSI
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
_fmblen
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
mbstowcs, mbtowc, wcstombs, wctomb, MB_CUR_MAX, MB_LEN_MAX
/* MBLEN.CPP illustrates the behavior of the mblen function. */
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int i;
char *pmbc = (char *)malloc( sizeof( char ) );
wchar_t wc = L'a';
printf( "Convert a wide character to multibyte character:\n" );
i = wctomb( pmbc, wc );
printf( "\tCharacters converted: %u\n", i );
printf( "\tMultibyte character: %x\n\n", pmbc );
printf( "Find length--in bytes--of multibyte character:\n" );
i = mblen( pmbc, MB_CUR_MAX );
printf( "\tLength--in bytes--of multibyte character: %u\n", i );
printf( "\tWide character: %x\n\n", pmbc );
printf( "Attempt to find length of a NULL pointer:\n" );
pmbc = NULL;
i = mblen( pmbc, MB_CUR_MAX );
printf( "\tLength--in bytes--of multibyte character: %u\n", i );
printf( "\tWide character: %x\n\n", pmbc );
printf( "Attempt to find length of a wide-character NULL:\n" );
wc = L'\0';
wctomb( pmbc, wc );
i = mblen( pmbc, MB_CUR_MAX );
printf( "\tLength--in bytes--of multibyte character: %u\n", i );
printf( "\tWide character: %x\n", pmbc );
}
Convert a wide character to multibyte character:
Characters converted: 1
Multibyte character: e56
Find length--in bytes--of multibyte character:
Length--in bytes--of multibyte character: 1
Wide character: e56
Attempt to find length of a NULL pointer:
Length--in bytes--of multibyte character: 0
Wide character: 0
Attempt to find length of a wide-character NULL:
Length--in bytes--of multibyte character: 0
Wide character: 0