mbsrtowcs() function in C/C++

The mbsrtowcs() function in C/C++ converts a narrow multibyte character sequence to a wide character sequence. It converts the multibyte character string whose first byte is represented by *src to corresponding wide character representation and is stored in the wide character array pointed to by dest. A maximum of len wide characters are written to dest.

Syntax :

size_t mbsrtowcs( wchar_t* dest, const char** src, size_t len, mbstate_t* ps )

Parameters: The function accepts four mandatory parameters which are described below:

Return value : The function returns two value as below:

Note : This function moves the src pointer to the end of the converted multibyte string. This doesn’t happen if dst==NULL



Below programs illustrate the above function :
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate 
// mbsrtowcs function
#include <bits/stdc++.h>
using namespace std;
   
int main()
{
    setlocale(LC_ALL, "en_US.utf8");
       
    // initializing the string
    // narrow multibyte character sequence
    const char* src = "\u0763\u0757";
    wchar_t dest[20];
       
    // initial state
    mbstate_t ps = mbstate_t();
       
    // maximum number of wide character
    int max = 10;
       
    int retVal = mbsrtowcs ( dest, &src, max, &ps );
    wcout << L"Number of wide characters written = "
          << retVal << endl;
     
    wcout << L"Wide character = " << dest << endl;
       
    return 0;
chevron_right

Output:
Number of wide characters written = 2
Wide character = Ý£Ý?

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate 
// mbsrtowcs function
#include <bits/stdc++.h>
using namespace std;
   
int main()
{
    // set locale
    setlocale(LC_ALL, "en_US.utf8");
       
    // initializing the string
    // narrow multibyte character sequence
    const char* src = u8"z\u00df\u6c34\U0001f34c";
    wchar_t dest[20];
       
    // initial state
    mbstate_t ps = mbstate_t();
       
    // maximum number of wide character
    int max = 10;
       
    int retVal = mbsrtowcs ( dest, &src, max, &ps );
    wcout << L"Number of wide characters written  = "
          << retVal << endl;
     
    wcout << L"Wide character = " << dest << endl;
       
    return 0;
}
chevron_right

Output:
Number of wide characters written  = 4
Wide character = zÃ?æ°´ð??

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C
Practice Tags :