Difference between revisions of "cpp/string/multibyte/mbstowcs"
(+see c) |
D41D8CD98F (Talk | contribs) m |
||
(10 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|mbstowcs}} | {{cpp/title|mbstowcs}} | ||
− | {{cpp/string/multibyte/ | + | {{cpp/string/multibyte/navbar}} |
− | {{ddcl | header=cstdlib | | + | {{ddcl|header=cstdlib| |
− | std::size_t mbstowcs( wchar_t* dst, const char* src, std::size_t len) | + | std::size_t mbstowcs( wchar_t* dst, const char* src, std::size_t len ); |
}} | }} | ||
− | Converts a multibyte character string from the array whose first element is pointed to by {{ | + | Converts a multibyte character string from the array whose first element is pointed to by {{c|src}} to its wide character representation. Converted characters are stored in the successive elements of the array pointed to by {{c|dst}}. No more than {{c|len}} wide characters are written to the destination array. |
− | + | ||
− | + | ||
+ | Each character is converted as if by a call to {{lc|std::mbtowc}}, except that the mbtowc conversion state is unaffected. The conversion stops if: | ||
* The multibyte null character was converted and stored. | * The multibyte null character was converted and stored. | ||
* An invalid (in the current C locale) multibyte character was encountered. | * An invalid (in the current C locale) multibyte character was encountered. | ||
− | * The next wide character to be stored would exceed {{ | + | * The next wide character to be stored would exceed {{c|len}}. |
===Notes=== | ===Notes=== | ||
− | In most implementations, this function updates a global static object of type {{ | + | In most implementations, this function updates a global static object of type {{lc|std::mbstate_t}} as it processes through the string, and cannot be called simultaneously by two threads, {{lc|std::mbsrtowcs}} should be used in such cases. |
− | POSIX specifies a common extension: if {{ | + | POSIX specifies a common extension: if {{c|dst}} is a null pointer, this function returns the number of wide characters that would be written to {{c|dst}}, if converted. Similar behavior is standard for {{lc|std::mbsrtowcs}}. |
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|dst|pointer to wide character array where the wide string will be stored}} |
− | {{ | + | {{par|src|pointer to the first element of a null-terminated multibyte string to convert}} |
− | {{ | + | {{par|len|number of wide characters available in the array pointed to by dst}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
On success, returns the number of wide characters, excluding the terminating {{c|L'\0'}}, written to the destination array. | On success, returns the number of wide characters, excluding the terminating {{c|L'\0'}}, written to the destination array. | ||
− | On conversion error (if invalid multibyte character was encountered), returns {{c|static_cast<std::size_t>(-1)}}. | + | On conversion error (if invalid multibyte character was encountered), returns {{c|static_cast<std::size_t> (-1)}}. |
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
− | + | ||
#include <clocale> | #include <clocale> | ||
#include <cstdlib> | #include <cstdlib> | ||
+ | #include <iostream> | ||
+ | |||
int main() | int main() | ||
{ | { | ||
std::setlocale(LC_ALL, "en_US.utf8"); | std::setlocale(LC_ALL, "en_US.utf8"); | ||
− | const char* mbstr = | + | std::wcout.imbue(std::locale("en_US.utf8")); |
− | // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\ | + | 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]; | wchar_t wstr[5]; | ||
std::mbstowcs(wstr, mbstr, 5); | std::mbstowcs(wstr, mbstr, 5); | ||
std::wcout << "wide string: " << wstr << '\n'; | std::wcout << "wide string: " << wstr << '\n'; | ||
} | } | ||
− | + | |output= | |
− | wide string: | + | wide string: zß水🍌 |
}} | }} | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/string/multibyte/dsc mbsrtowcs}} |
− | {{ | + | {{dsc inc|cpp/string/multibyte/dsc wcstombs}} |
− | {{ | + | {{dsc inc|cpp/locale/codecvt/dsc do_in}} |
− | {{ | + | {{dsc see c|c/string/multibyte/mbstowcs}} |
− | {{ | + | {{dsc end}} |
+ | |||
+ | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 10:01, 19 October 2023
Defined in header <cstdlib>
|
||
std::size_t mbstowcs( wchar_t* dst, const char* src, std::size_t len ); |
||
Converts a multibyte character string from the array whose first element is pointed to by src to its wide character representation. 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.
Each character is converted as if by a call to std::mbtowc, except that the mbtowc conversion state is unaffected. 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.
Contents |
[edit] 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::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.
[edit] 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 by dst |
[edit] Return value
On success, returns the number of wide characters, excluding the terminating L'\0', written to the destination array.
On conversion error (if invalid multibyte character was encountered), returns static_cast<std::size_t> (-1).
[edit] Example
#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ß水🍌
[edit] See also
converts a narrow multibyte character string to wide string, given state (function) | |
converts a wide string to narrow multibyte character string (function) | |
[virtual] |
converts a string from ExternT to InternT , such as when reading from file (virtual protected member function of std::codecvt<InternT,ExternT,StateT> )
|
C documentation for mbstowcs
|