std::mbstowcs(3)
π NAME π
okay bestie, meet std::mbstowcs β literally std::mbstowcs, no notes. It's from the C++ Standard Library (std::mbstowcs(3) if you're keeping score), and the original header spells it C++ Standard Libary, which is honestly iconic of it. This function is giving "turn my multibyte string into wide characters" energy, and I'm obsessed. π«Ά
β¨ Synopsis β¨
Defined in header <cstdlib>:
std::size_t mbstowcs( wchar_t* dst, const char* src, std::size_t len );
okay so here's the vibe: it converts a multibyte character string from the array whose first element is pointed to by src into its wide character representation. The converted characters are stored in the successive elements of the array pointed to by dst. No more than len wide characters are written to the destination array. That's the cap, bestie. π
Each character is converted as if by a call to std::mbtowc, except that the mbtowc conversion state is unaffected. iykyk π§ The conversion stops if:
- The multibyte null character was converted and stored.
- An invalid (in the current C locale) multibyte character was encountered.
- The next wide character to be stored would exceed
len.
πΈ Notes πΈ
In most implementations, this function updates a global static object of type std::mbstate_t as it processes through the string, and it cannot be called simultaneously by two threads. Not me being a shared-state main character π β std::mbsrtowcs should be used in such cases.
POSIX specifies a common extension: if dst is a null pointer, this function returns the number of wide characters that would be written to dst, if converted. Similar behavior is standard for std::mbsrtowcs. slay for consistency β¨
π Parameters π
dstβ pointer to wide character array where the wide string will be stored.srcβ pointer to the first element of a null-terminated multibyte string to convert.lenβ number of wide characters available in the array pointed to bydst.
π¦ Return value π¦
On success, it returns the number of wide characters, excluding the terminating L'\0', written to the destination array. periodt. π
On conversion error (if an invalid multibyte character was encountered), it returns static_cast<std::size_t> (-1). yeah, that's the sad little error vibe check π
π Example π
okay, run this code πββοΈπ¨ (the source even says // Run this code):
#include <clocale>
#include <cstdlib>
#include <iostream>
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::wcout.imbue(std::locale("en_US.utf8"));
const char* mbstr = "z\u00df\u6c34\U0001f34c"; // or u8"zΓζ°΄π"
// or
"\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
wchar_t wstr[5];
std::mbstowcs(wstr, mbstr, 5);
std::wcout << "wide string: " << wstr << '\n';
}
Output:
wide string: zΓζ°΄π
π See also π
mbsrtowcsβ converts a narrow multibyte character string to wide string, given state (function).wcstombsβ converts a wide string to narrow multibyte character string (function).do_inβ converts a string fromExternTtoInternT, such as when reading from file [virtual] (virtual protected member function ofstd::codecvt<InternT,ExternT,StateT>).- C documentation for
mbstowcs.
kisses, your Unix bestie π β this page came from cppreference.com, version 2024.06.10, and it's the std::mbstowcs(3) manual page. periodt. π
π¦ 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