std::wcstombs

Header: <cstdlib>

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 initial shift state. 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.

# Declarations

std::size_t wcstombs( char* dst, const wchar_t* src, std::size_t len );

# Parameters

# 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.

# Notes

In most implementations, this function updates a global static object of type std::mbstate_t as it processes through the string, and cannot be called simultaneously by two threads, std::wcsrtombs should be used in such cases.

POSIX specifies a common extension: if dst is a null pointer, this function returns the number of bytes that would be written to dst, if converted. Similar behavior is standard for std::wcsrtombs.

# Example

#include <clocale>
#include <cstdlib>
#include <iostream>
 
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ß水𝄋"
    char mbstr[11];
    std::wcstombs(mbstr, wstr, 11);
    std::cout << "multibyte string: " << mbstr << '\n';
}

# See also