mbrtowc() function in C/C++ program


In this article we will be discussing the working, syntax and examples of std::mbrtowc() function in C++ STL.

What is std::mbrtowc()?

std::mbrtowc() function is an inbuilt function in C++ STL, which is defined in the <cwchar> header file. mbrtowc() means that it converts the narrow multibyte character string to wide character. This function is used to convert a narrow multibyte character to wide character representation.

Syntax

size_t mbrtowc( wchar_t* pwc, char* str, size_t n, mbstate_t* ps);

Parameters

The function accepts following parameter(s) −

  • pwc − This is the pointer to the location we want the output to be stored.
  • str − Character string which is used as the input.
  • n − It is the number of bytes that are to be checked.
  • ps − It is the pointer to the state object when we are interpreting the multibyte string.

Return value

This function return values differ as per the following condition −

  • 0 − The function will return zero when the character in str which has to be converted is NULL.
  • 1…n − The number of bytes of multibyte character which are converted from the character string *str.
  • -2 − We will get -2 when next n bytes incomplete but so far is a valid multibyte character.
  • -1 − We get -1 when we face an encoding error, nothing is written to *pwc.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_(const char* ch){
   mbstate_t temp = mbstate_t();
   int cal = strlen(ch);
   const char* i = ch + cal;
   int total;
   wchar_t con;
   while ((total = mbrtowc(&con, ch, i - ch, &temp)) > 0){
      wcout << "Next " << total <<" bytes are the character " << con << '\n';
      ch += total;
   }
}
int main(){
   setlocale(LC_ALL, "en_US.utf8");
   const char* len = u8"z\u00df\u6c34";
   print_(len);
}

Output

Next 1 bytes are the character z
Next 2 bytes are the character ß
Next 3 bytes are the character 水

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_(const char* ch){
   mbstate_t temp = mbstate_t();
   int cal = strlen(ch);
   const char* i = ch + cal;
   int total;
   wchar_t con;
   while ((total = mbrtowc(&con, ch, i - ch, &temp)) > 0){
      wcout << "Next " << total <<" bytes are the character " << con << '\n';
      ch += total;
   }
}
int main(){
   setlocale(LC_ALL, "en_US.utf8");
   const char* len = u8"\xE2\x88\x83y\xE2\x88\x80x\xC2";
   print_(len);
}

Output

Next 3 bytes are the character ∃
Next 1 bytes are the character y
Next 3 bytes are the character ∀
Next 1 bytes are the character x

Updated on: 17-Apr-2020

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements