Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Smita Kapse

This mbrtowc() function is used to convert multibyte sequence to wide character string. This returns the length of the multibyte characters in byte. The syntax is like below.

mbrtowc (wchar_t* wc, const char* s, size_t max, mbstate_t* ps)

The arguments are −

  • wc is the pointer which points where the resulting wide character will be stored.
  • s is the pointer to multibyte character string as input
  • max is the maximum number of bytes in s, that can be examined
  • ps is pointing to the conversion state, when interpreting multibyte string.

Example

#include <bits/stdc++.h>
using namespace std;
void display(const char* s) {
   mbstate_t ps = mbstate_t(); // initial state
   int s_len = strlen(s);
   const char* n = s + s_len;
   int len;
   wchar_t wide_char;
   while ((len = mbrtowc(&wide_char, s, n - s, &ps)) > 0) {
      wcout << "The following " << len << " bytes are for the character " << wide_char << '\n';
      s += len;
   }
}
main() {
   setlocale(LC_ALL, "en_US.utf8");
   const char* str = u8"z\u00cf\u7c38\U00000915";
   display(str);
}

Output

The following 1 bytes are for the character z
The following 2 bytes are for the character Ï
The following 3 bytes are for the character 簸
The following 3 bytes are for the character क

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.