Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/multibyte/wcstombs"

From cppreference.com
< cpp‎ | string‎ | multibyte
m (Text replace - "/sidebar" to "/navbar")
m
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{cpp/title|wcstombs}}
 
{{cpp/title|wcstombs}}
 
{{cpp/string/multibyte/navbar}}
 
{{cpp/string/multibyte/navbar}}
{{ddcl | header=cstdlib |
+
{{ddcl|header=cstdlib|
std::size_t wcstombs( char* dst, const wchar_t* src, std::size_t len)
+
std::size_t wcstombs( char* dst, const wchar_t* src, std::size_t len );
 
}}
 
}}
  
Converts a sequence of wide characters from the array whose first element is pointed to by {{tt|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 {{tt|dst}}. No more than {{tt|len}} bytes are written to the destination array.
+
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 initial shift state. 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 {{c|std::wctomb}}, except that the wctomb's conversion state is unaffected. The conversion stops if:
+
  
 +
Each character is converted as if by a call to {{lc|std::wctomb}}, except that the wctomb's conversion state is unaffected. The conversion stops if:
 
* The null character was converted and stored.
 
* 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.
 
* 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 {{tt|len}}.
+
* The next multibyte character to be stored would exceed {{c|len}}.
  
 
===Notes===
 
===Notes===
In most implementations, this function updates a global static object of type {{c|std::mbstate_t}} as it processes through the string, and cannot be called simultaneously by two threads, {{c|std::wcsrtombs}} should be used in such cases.
+
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, {{c|std::wcsrtombs}} should be used in such cases.
  
POSIX specifies a common extension: if {{tt|dst}} is a null pointer, this function returns the number of bytes that would be written to {{tt|dst}}, if converted. Similar behavior is standard for {{c|std::wcsrtombs}}.
+
POSIX specifies a common extension: if {{c|dst}} is a null pointer, this function returns the number of bytes that would be written to {{c|dst}}, if converted. Similar behavior is standard for {{c|std::wcsrtombs}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | dst | pointer to narrow character array where the multibyte character will be stored}}
+
{{par|dst|pointer to narrow character array where the multibyte character will be stored}}
{{param list item | src | pointer to the first element of a null-terminated wide string to convert}}
+
{{par|src|pointer to the first element of a null-terminated wide string to convert}}
{{param list item | len | number of byte available in the array pointed to by dst}}
+
{{par|len|number of byte available in the array pointed to by dst}}
{{param list 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 {{tt|dst}}.
+
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}}.
  
 
On conversion error (if invalid wide character was encountered), returns {{c|static_cast<std::size_t>(-1)}}.
 
On conversion error (if invalid wide character was encountered), returns {{c|static_cast<std::size_t>(-1)}}.
Line 32: Line 31:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
 
#include <clocale>
 
#include <clocale>
 
#include <cstdlib>
 
#include <cstdlib>
 +
#include <iostream>
  
 
int main()
 
int main()
Line 47: Line 45:
 
     std::cout << "multibyte string: " << mbstr << '\n';
 
     std::cout << "multibyte string: " << mbstr << '\n';
 
}
 
}
| output=
+
|output=
 
multibyte string: zß水𝄋
 
multibyte string: zß水𝄋
 
}}
 
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/multibyte/dcl list wcsrtombs}}
+
{{dsc inc|cpp/string/multibyte/dsc wcsrtombs}}
{{dcl list template | cpp/string/multibyte/dcl list mbstowcs}}
+
{{dsc inc|cpp/string/multibyte/dsc mbstowcs}}
{{dcl list template | cpp/locale/codecvt/dcl list do_out}}
+
{{dsc inc|cpp/locale/codecvt/dsc do_out}}
{{dcl list see c | c/string/multibyte/wcstombs}}
+
{{dsc see c|c/string/multibyte/wcstombs}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 10:04, 19 October 2023

Defined in header <cstdlib>
std::size_t wcstombs( char* dst, const wchar_t* src, std::size_t len );

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.

Each character is converted as if by a call to std::wctomb, except that the wctomb's conversion state is unaffected. The conversion stops if:

  • The null character was converted and stored.
  • A 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 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::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.

[edit] Parameters

dst - pointer to narrow character array where the multibyte character will be stored
src - pointer to the first element of a null-terminated wide string to convert
len - number of byte available in the array pointed to by dst

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

On conversion error (if invalid wide 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");
    // 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';
}

Output:

multibyte string: zß水𝄋

[edit] See also

converts a wide string to narrow multibyte character string, given state
(function) [edit]
converts a narrow multibyte character string to wide string
(function) [edit]
[virtual]
converts a string from InternT to ExternT, such as when writing to file
(virtual protected member function of std::codecvt<InternT,ExternT,StateT>) [edit]
C documentation for wcstombs