std::wcsrtombs(3)
okay so bestie, welcome to std::wcsrtombs(3) from the C++ Standard Libary manual β this page is literally about converting wide characters into a narrow multibyte string, and it's giving main character energy. π
β¨
π NAME π
std::wcsrtombs - std::wcsrtombs β that's the name, no notes, periodt. π
πΈ Synopsis πΈ
Defined in header <cwchar>
std::size_t wcsrtombs( char* dst,
const wchar_t** src,
std::size_t len,
std::mbstate_t* ps );
Okay so what does it actually do? It converts a sequence of wide characters from the array whose first element is pointed to by *src to its narrow multibyte representation that begins in the conversion state described by *ps. If dst is not null, converted characters are stored in the successive elements of the char array pointed to by dst. No more than len bytes are written to the destination array. π«Ά
Each character is converted as if by a call to std::wcrtomb. The conversion stops if:
- The null character was converted and stored.
srcis set to a null pointer and*psrepresents the initial shift state. - A
wchar_twas found that does not correspond to a valid character in the current C locale.srcis set to point at the first unconverted wide character. - The next multibyte character to be stored would exceed
len.srcis set to point at the first unconverted wide character. This condition is not checked ifdstis a null pointer.
π¨ bestie, that last bullet is a real caveat: the condition about exceedinglenis not checked ifdstis a null pointer.
π Parameters π
dstβ pointer to narrow character array where the multibyte characters will be stored. πsrcβ pointer to pointer to the first element of a null-terminated wide string. π¦lenβ number of bytes available in the array pointed to bydst. πpsβ pointer to the conversion state object. β¨
π Return value π
On success, returns the number of bytes (including any shift sequences, but excluding the terminating '\0') written to the character array whose first element is pointed to by dst. If dst is a null pointer, returns the number of bytes that would have been written (again, excluding the terminating null character '\0'). π
On conversion error (if invalid wide character was encountered), returns static_cast<std::size_t>(-1), stores EILSEQ in errno, and leaves *ps in unspecified state. ππ
π¦ Example π¦
// Run this code
#include <clocale>
#include <cwchar>
#include <iostream>
#include <string>
#include <vector>
void print_wide(const wchar_t* wstr)
{
std::mbstate_t state = std::mbstate_t();
std::size_t len = 1 + std::wcsrtombs(nullptr, &wstr, 0, &state);
std::vector<char> mbstr(len);
std::wcsrtombs(&mbstr[0], &wstr, mbstr.size(), &state);
std::cout << "multibyte string: " << &mbstr[0] << '\n'
<< "Length, including '\\0': " << mbstr.size() << '\n';
}
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
// UTF-8 narrow multibyte encoding
const wchar_t* wstr = L"z\u00df\u6c34\U0001d10b"; // or L"zΓζ°΄π"
print_wide(wstr);
}
π Output π
multibyte string: zΓζ°΄π
Length, including '\0': 11
π₯ See also π₯
wcrtombβ converts a wide character to its multibyte representation, given state (function)mbsrtowcsβ converts a narrow multibyte character string to wide string, 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
wcsrtombs
source: http://cppreference.com β’ version/date: 2024.06.10 β’ page: std::wcsrtombs(3) β bye bestie, go convert something cute ππ¦
π¦ 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