std::wctomb(3)
π std::wctomb(3) β C++ Standard Libary, bestie
πΈ NAME
std::wctomb β std::wctomb. That's the name, periodt. We're keeping the double-name energy exactly as the original served it. π
β¨ Synopsis
Okay so first things first: this icon lives in the header <cstdlib>, and the signature is literally:
int wctomb( char* s, wchar_t wc );
The way this works: it converts a wide character wc to multibyte encoding and stores it β including any shift sequences β in the char array whose first element is pointed to by s. No more than MB_CUR_MAX characters are stored. The conversion is affected by the current locale's LC_CTYPE category.
If wc is the null character, the null byte is written to s, preceded by any shift sequences necessary to restore the initial shift state.
If s is a null pointer, it resets the global conversion state and determines whether shift sequences are used. π¦
π¦ Parameters
sβ pointer to the character array for output.wcβ wide character to convert.
π Return value
If s is not a null pointer, it returns the number of bytes that are contained in the multibyte representation of wc, or -1 if wc is not a valid character.
If s is a null pointer, it resets its internal conversion state to represent the initial shift state and returns 0 if the current multibyte encoding is not state-dependent (does not use shift sequences), or a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences). Slay. β¨
π§ Notes
Each call to wctomb updates the internal global conversion state (a static object of type std::mbstate_t, only known to this function). If the multibyte encoding uses shift states, this function is not reentrant.
In any case, multiple threads should not callwctombwithout synchronization:std::wcrtombmay be used instead. π This is not the "just vibe and call it from every thread" situation, bestie.
π Example
The original has a // Run this code label right here, so consider this your cue:
#include <clocale>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
void print_wide(const std::wstring& wstr)
{
bool shifts = std::wctomb(nullptr, 0); // reset the conversion state
std::cout << "shift sequences are " << (shifts ? "" : "not" )
<< " used\n" << std::uppercase << std::setfill('0');
for (const wchar_t wc : wstr)
{
std::string mb(MB_CUR_MAX, '\0');
const int ret = std::wctomb(&mb[0], wc);
const char* s = ret > 1 ? "s" : "";
std::cout << "multibyte char '" << mb << "' is " << ret
<< " byte" << s << ": [" << std::hex;
for (int i{0}; i != ret; ++i)
{
const int c = 0xFF & mb[i];
std::cout << (i ? " " : "") << std::setw(2) << c;
}
std::cout << "]\n" << std::dec;
}
}
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
// UTF-8 narrow multibyte encoding
std::wstring wstr = L"z\u00df\u6c34\U0001d10b"; // or L"zΓζ°΄π"
print_wide(wstr);
}
π€ Output
shift sequences are not used
multibyte char 'z' is 1 byte: [7A]
multibyte char 'Γ' is 2 bytes: [C3 9F]
multibyte char 'ζ°΄' is 3 bytes: [E6 B0 B4]
multibyte char 'π' is 4 bytes: [F0 9D 84 8B]
π₯ See also
mbtowcβ converts the next multibyte character to wide character (function).wcrtombβ converts a wide character to its multibyte representation, 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
wctombβ yep, the C version, iykyk.
Signing off from http://cppreference.com π version 2024.06.10, on the std::wctomb(3) page. 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