SysTutorials

Linux Manual Pages › Section 3

std-codecvt-3(3)

NAME

std::mbrtowc - std::mbrtowc

Synopsis

Defined in header <cwchar> std::size_t mbrtowc( wchar_t* pwc, const char* s, std::size_t n, std::mbstate_t* ps );

Converts a narrow multibyte character to a wide character. If s is not a null pointer, inspects at most n bytes of the multibyte character string, beginning with the byte pointed to by s to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character in s is complete and valid, converts it to the corresponding wide character and stores it in *pwc (if pwc is not null). If s is a null pointer, the values of n and pwc are ignored and call is equivalent to std::mbrtowc(NULL, "", 1, ps). If the wide character produced is the null character, the conversion state stored in *ps is the initial shift state.

Parameters

pwc - pointer to the location where the resulting wide character will be written s - pointer to the multibyte character string used as input n - limit on the number of bytes in s that can be examined ps - pointer to the conversion state used when interpreting the multibyte string

Return value

The first of the following that applies:

Example

// Run this code

  #include <iostream>   #include <clocale>   #include <cstring>   #include <cwchar>

  void print_mb(const char* ptr)   {       std::mbstate_t state = std::mbstate_t(); // initial state       const char* end = ptr + std::strlen(ptr);       int len;       wchar_t wc;       while((len = std::mbrtowc(&wc, ptr, end-ptr, &state)) > 0) {           std::wcout << "Next " << len << " bytes are the character " << wc << ‘\n’;           ptr += len;       }   }

  int main()   {       std::setlocale(LC_ALL, "en_US.utf8");       // UTF-8 narrow multibyte encoding       const char* str = u8"z\u00df\u6c34\U0001d10b"; // or u8"zß水"                         // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";       print_mb(str);   }

Output:

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

See also

          converts the next multibyte character to wide character mbtowc (function)           converts a wide character to its multibyte representation, given state wcrtomb (function)

do_in converts a string from externT to internT, such as when reading from file           (virtual protected member function of std::codecvt<InternT,ExternT,State>) [virtual]