[Previous] [Contents] [Next]

mbstowcs()

Convert multibyte characters into wide characters

Synopsis:

#include <stdlib.h>

size_t mbstowcs( wchar_t * pwcs,
                 const char * s,
                 size_t n );

Library:

libc

Description:

The mbstowcs() function converts a sequence of multibyte characters pointed to by s into their corresponding wide-character codes pointed to by pwcs, to a maximum of n bytes. It doesn't convert any multibyte characters beyond a NULL character.

This function is affected by LC_TYPE.

Returns:

(size_t)-1
Failure; invalid multibyte character encountered.
x
Success; the number of array elements modified, not including the terminating zero code, if present.

Errors:

EILSEQ
Invalid character sequence.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char *wc = "string";
    wchar_t wbuffer[50];
    int i, len;

    len = mbstowcs( wbuffer, wc, 50 );
    if( len != -1 ) {
        wbuffer[len] = '\0';
        printf( "%s(%d)\n", wc, len );
        
        for( i = 0; i < len; i++ ) {
            printf( "/%4.4x", wbuffer[i] );
        }
        
        printf( "\n" );
    }
    
    return EXIT_SUCCESS;
}

This produces the output:

string(6)
/0073/0074/0072/0069/006e/0067

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

errno()

"Multibyte character functions" and "Wide-character functions" in the summary of functions chapter.


[Previous] [Contents] [Next]