Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | multibyte
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (Shorten template names. Use {{lc}} where appropriate.)
Line 7: Line 7:
 
Converts a multibyte character string from the array whose first element is pointed to by {{tt|src}} to its wide character  representation. Converted characters are stored in the successive elements of the array pointed to by {{tt|dst}}. No more than {{tt|len}} wide characters are written to the destination array.
 
Converts a multibyte character string from the array whose first element is pointed to by {{tt|src}} to its wide character  representation. Converted characters are stored in the successive elements of the array pointed to by {{tt|dst}}. No more than {{tt|len}} wide characters are written to the destination array.
  
Each character is converted as if by a call to {{c|std::mbtowc}}, except that the mbtowc conversion state is unaffected. The conversion stops if:
+
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.
 
* The multibyte null character was converted and stored.
Line 14: Line 14:
  
 
===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::mbsrtowcs}} should be used in such cases.
+
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 {{tt|dst}} is a null pointer, this function returns the number of wide characters that would be written to {{tt|dst}}, if converted. Similar behavior is standard for {{c|std::mbsrtowcs}}.
+
POSIX specifies a common extension: if {{tt|dst}} is a null pointer, this function returns the number of wide characters that would be written to {{tt|dst}}, if converted. Similar behavior is standard for {{|std::mbsrtowcs}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{begin}}
{{param list item | dst | pointer to wide character array where the wide string will be stored}}
+
{{| dst | pointer to wide character array where the wide string will be stored}}
{{param list item | src | pointer to the first element of a null-terminated multibyte string to convert}}
+
{{| src | pointer to the first element of a null-terminated multibyte string to convert}}
{{param list item | len | number of wide characters available in the array pointed to by dst}}
+
{{| len | number of wide characters available in the array pointed to by dst}}
{{param list end}}
+
{{end}}
  
 
===Return value===
 
===Return value===
Line 51: Line 51:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | cpp/string/multibyte/dcl list mbsrtowcs}}
+
{{| cpp/string/multibyte/dcl list mbsrtowcs}}
{{dcl list template | cpp/string/multibyte/dcl list wcstombs}}
+
{{| cpp/string/multibyte/dcl list wcstombs}}
{{dcl list template | cpp/locale/codecvt/dcl list do_in}}
+
{{| cpp/locale/codecvt/dcl list do_in}}
{{dcl list see c | c/string/multibyte/mbstowcs}}
+
{{see c | c/string/multibyte/mbstowcs}}
{{dcl list end}}
+
{{end}}
  
 
[[de:cpp/string/multibyte/mbstowcs]]
 
[[de:cpp/string/multibyte/mbstowcs]]

Revision as of 20:57, 31 May 2013

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

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.

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

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

Example

#include <iostream>
#include <clocale>
#include <cstdlib>
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    const char* mbstr = u8"z\u00df\u6c34\U0001d10b"; // or u8"zß水𝄋"
                        // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b";
    wchar_t wstr[5];
    std::mbstowcs(wstr, mbstr, 5);
    std::wcout << "wide string: " << wstr << '\n';
}

Output:

wide string: zß水𝄋

See also

Template:cpp/string/multibyte/dcl list mbsrtowcsTemplate:cpp/string/multibyte/dcl list wcstombsTemplate:cpp/locale/codecvt/dcl list do in
C documentation for mbstowcs