std::wcrtomb(3)
πΈ NAME πΈ
hey bestie, welcome to the std::wcrtomb(3) page from the C++ Standard Libary manual π this one is literally called std::wcrtomb β that's the name, that's the vibe, periodt.
β¨ Synopsis β¨
First things first: it's defined in header <cwchar> π
std::size_t wcrtomb( char* s, wchar_t wc, std::mbstate_t* ps );
Okay so std::wcrtomb converts a wide character to its narrow multibyte representation. Main character energy for real.
If s is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation of wc (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 by s, updating *ps as necessary. At most MB_CUR_MAX bytes can be written by this function. No cap, that's the limit.
If s is a null pointer, the call is equivalent to std::wcrtomb(buf, L'\0', ps) for some internal buffer buf. Cute little internal-buffer moment π
If wc is the null wide character L'\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. It's giving reset-to-default, iconic.
π Parameters π
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 π
π Return value π
On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by s. Slay, it tells you how many bytes it used β¨
On failure (if wc is not a valid wide character), returns static_cast<std::size_t>(-1), stores EILSEQ in errno, and leaves *ps in unspecified state. That's the "it didn't work and *ps is not okay" path, so don't act surprised π
π Example π
The page says // Run this code, so bestie, run this code:
#include <clocale>
#include <cwchar>
#include <iostream>
#include <string>
void print_wide(const std::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 bytes
multibyte char Γ is 2 bytes
multibyte char ζ°΄ is 3 bytes
multibyte char π is 4 bytes
π See also π
wctombβ converts a wide character to its multibyte representation (function) π§mbrtowcβ converts the next multibyte character to wide character, given state (function) π¦do_outβ converts a string fromInternTtoExternT, such as when writing to file β[virtual](virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>) π₯- C documentation for
wcrtombπ
that's the whole page, bestie β source http://cppreference.com, date 2024.06.10, mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms