Localization library | |||||||||||||||||||||||||
Regular expressions library(C++11) | |||||||||||||||||||||||||
Formatting library(C++20) | |||||||||||||||||||||||||
Null-terminated sequence utilities | |||||||||||||||||||||||||
Byte strings | |||||||||||||||||||||||||
Multibyte strings | |||||||||||||||||||||||||
Wide strings | |||||||||||||||||||||||||
Primitive numeric conversions | |||||||||||||||||||||||||
| |||||||||||||||||||||||||
Text encoding identifications | |||||||||||||||||||||||||
|
Defined in header <cwchar> | ||
std::size_t wcrtomb(char* s,wchar_t wc,std::mbstate_t* ps); | ||
Converts a wide character to its narrow multibyte representation.
Ifs is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation ofwc (including any shift sequences, and taking into account the current multibyte conversion state*ps), and stores the multibyte character representation in the character array whose first element is pointed to bys, updating*ps as necessary. At mostMB_CUR_MAX bytes can be written by this function.
Ifs is a null pointer, the call is equivalent tostd::wcrtomb(buf, L'\0', ps) for some internal bufferbuf
.
If wc is the null wide characterL'\0', a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter*ps is updated to represent the initial shift state.
Contents |
s | - | pointer to narrow character array where the multibyte character will be stored |
wc | - | the wide character to convert |
ps | - | pointer to the conversion state object used when interpreting the multibyte string |
On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to bys.
On failure (ifwc is not a valid wide character), returnsstatic_cast<std::size_t>(-1), storesEILSEQ inerrno, and leaves*ps in unspecified state.
#include <clocale>#include <cwchar>#include <iostream>#include <string> void print_wide(conststd::wstring& wstr){std::mbstate_t state{};for(wchar_t wc: wstr){std::string mb(MB_CUR_MAX,'\0');std::size_t ret= std::wcrtomb(&mb[0], wc,&state);std::cout<<"multibyte char "<< mb<<" is "<< ret<<" bytes\n";}} int main(){std::setlocale(LC_ALL,"en_US.utf8");std::wstring wstr= L"z\u00df\u6c34\U0001f34c";// or L"zß水🍌" print_wide(wstr);}
Output:
multibyte char z is 1 bytesmultibyte char ß is 2 bytesmultibyte char 水 is 3 bytesmultibyte char 🍌 is 4 bytes
converts a wide character to its multibyte representation (function)[edit] | |
converts the next multibyte character to wide character, given state (function)[edit] | |
[virtual] | converts a string fromInternT toExternT , such as when writing to file(virtual protected member function of std::codecvt<InternT,ExternT,StateT> )[edit] |
C documentation forwcrtomb |