The function mbstowcs () in C

Function size_t mbstowcs (* pwcs, const char * str, n) Converts the sequence of multi-byte char points pointed to by the parameter str to the array pointed to by pwcs.

Function size_t mbstowcs (* pwcs, const char * str, n) Converts the sequence of multi-byte char points pointed to by the parameter str to the array pointed to by pwcs .

Declare the function mbstowcs () in C

Below is the declaration for mbstowcs () in C:

 size_t mbstowcs ( * pwcs , const char * str , n ) 

Parameters

pwcs : This is a pointer to an array of wchar_t elements.

str : This is a multi-byte char string to be interpreted.

n : This is the maximum number of wchar_t characters to be interpreted.

Returns the value

This function returns the number of characters interpreted, except for the ending null character. If you encounter an invalid multi-byte char, this function returns -1.

For example

The following C program illustrates the usage of mbstowcs () in C:

 #include #include #include 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 ( "Chuyen doi thanh multibyte stringn" ); len = wcstombs ( pmb , pwc , MB_CUR_MAX ); printf ( "Cac ky tu duoc chuyen doi %dn" , len ); printf ( "Gia tri Hex cua multibyte char dau tien la: %#.4xn" , pmb ); printf ( "Chuyen doi nguoc lai thanh chuoi Wide-Charactern" ); len = mbstowcs ( pwcs , pmb , MB_CUR_MAX ); printf ( "Cac ky tu duoc chuyen doi %dn" , len ); printf ( "Gia tri Hex cua wide char dau tien la: %#.4xnn" , pwcs ); return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The function mbstowcs () in C

According to Tutorialspoint

Previous article: Function mblen () in C

Next lesson: The function mbtowc () in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile