Difference between revisions of "cpp/string/multibyte/wcsrtombs"
m (use std::size_t) |
Andreas Krug (Talk | contribs) m ({{c}}, capitalized 1st letter, headers sorted) |
||
(One intermediate revision by one user not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|wcsrtombs}} | {{cpp/title|wcsrtombs}} | ||
{{cpp/string/multibyte/navbar}} | {{cpp/string/multibyte/navbar}} | ||
− | {{ddcl | header=cwchar | | + | {{ddcl|header=cwchar| |
std::size_t wcsrtombs( char* dst, | std::size_t wcsrtombs( char* dst, | ||
const wchar_t** src, | const wchar_t** src, | ||
Line 8: | Line 8: | ||
}} | }} | ||
− | Converts a sequence of wide characters from the array whose first element is pointed to by {{ | + | Converts a sequence of wide characters from the array whose first element is pointed to by {{c|*src}} to its narrow multibyte representation that begins in the conversion state described by {{c|*ps}}. If {{c|dst}} is not null, converted characters are stored in the successive elements of the char array pointed to by {{c|dst}}. No more than {{c|len}} bytes are written to the destination array. |
Each character is converted as if by a call to {{lc|std::wcrtomb}}. The conversion stops if: | Each character is converted as if by a call to {{lc|std::wcrtomb}}. The conversion stops if: | ||
− | + | * The null character was converted and stored. {{c|src}} is set to a null pointer and {{c|*ps}} represents the initial shift state. | |
− | * The null character was converted and stored. {{ | + | * A {{c|wchar_t}} was found that does not correspond to a valid character in the current C locale. {{c|src}} is set to point at the first unconverted wide character. |
− | * A {{c|wchar_t}} was found that does not correspond to a valid character in the current C locale. {{ | + | * The next multibyte character to be stored would exceed {{c|len}}. {{c|src}} is set to point at the first unconverted wide character. This condition is not checked if {{c|dst}} is a null pointer. |
− | * | + | |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | dst | pointer to narrow character array where the multibyte characters will be stored}} | + | {{par|dst|pointer to narrow character array where the multibyte characters will be stored}} |
− | {{par | src | pointer to pointer to the first element of a null-terminated wide string}} | + | {{par|src|pointer to pointer to the first element of a null-terminated wide string}} |
− | {{par | len | number of bytes available in the array pointed to by dst}} | + | {{par|len|number of bytes available in the array pointed to by dst}} |
− | {{par | ps | pointer to the conversion state object}} | + | {{par|ps|pointer to the conversion state object}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | On success, returns the number of bytes (including any shift sequences, but excluding the terminating {{c|'\0'}}) written to the character array whose first element is pointed to by {{ | + | On success, returns the number of bytes (including any shift sequences, but excluding the terminating {{c|'\0'}}) written to the character array whose first element is pointed to by {{c|dst}}. If {{c|dst}} is a null pointer, returns the number of bytes that would have been written (again, excluding the terminating null character {{c|'\0'}}). |
On conversion error (if invalid wide character was encountered), returns {{c|static_cast<std::size_t>(-1)}}, stores {{lc|EILSEQ}} in {{lc|errno}}, and leaves {{c|*ps}} in unspecified state. | On conversion error (if invalid wide character was encountered), returns {{c|static_cast<std::size_t>(-1)}}, stores {{lc|EILSEQ}} in {{lc|errno}}, and leaves {{c|*ps}} in unspecified state. | ||
Line 31: | Line 30: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | #include <clocale> | |
+ | #include <cwchar> | ||
#include <iostream> | #include <iostream> | ||
+ | #include <string> | ||
#include <vector> | #include <vector> | ||
− | |||
− | |||
− | |||
void print_wide(const wchar_t* wstr) | void print_wide(const wchar_t* wstr) | ||
Line 56: | Line 54: | ||
print_wide(wstr); | print_wide(wstr); | ||
} | } | ||
− | + | |output= | |
multibyte string: zß水𝄋 | multibyte string: zß水𝄋 | ||
Length, including '\0': 11 | Length, including '\0': 11 | ||
Line 63: | Line 61: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/string/multibyte/dsc wcrtomb}} | + | {{dsc inc|cpp/string/multibyte/dsc wcrtomb}} |
− | {{dsc inc | cpp/string/multibyte/dsc mbsrtowcs}} | + | {{dsc inc|cpp/string/multibyte/dsc mbsrtowcs}} |
− | {{dsc inc | cpp/locale/codecvt/dsc do_out}} | + | {{dsc inc|cpp/locale/codecvt/dsc do_out}} |
− | {{dsc see c | c/string/multibyte/wcsrtombs}} | + | {{dsc see c|c/string/multibyte/wcsrtombs}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 22:44, 9 June 2023
Defined in header <cwchar>
|
||
std::size_t wcsrtombs( char* dst, const wchar_t** src, |
||
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. src is set to a null pointer and *ps represents the initial shift state.
- A wchar_t was found that does not correspond to a valid character in the current C locale. src is set to point at the first unconverted wide character.
- The next multibyte character to be stored would exceed len. src is set to point at the first unconverted wide character. This condition is not checked if dst is a null pointer.
Contents |
[edit] 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 by dst |
ps | - | pointer to the conversion state object |
[edit] 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.
[edit] Example
#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
[edit] See also
converts a wide character to its multibyte representation, given state (function) | |
converts a narrow multibyte character string to wide string, given state (function) | |
[virtual] |
converts a string from InternT to ExternT , such as when writing to file (virtual protected member function of std::codecvt<InternT,ExternT,StateT> )
|
C documentation for wcsrtombs
|