Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | multibyte
m (Corrected the wrong function name(c16rtomb->c32rtomb).)
m ({{c}}, headers sorted, fmt)
 
(16 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{cpp/title|c32rtomb}}
 
{{cpp/title|c32rtomb}}
 
{{cpp/string/multibyte/navbar}}
 
{{cpp/string/multibyte/navbar}}
{{ddcl | header=cuchar | notes={{mark since c++11}} |
+
{{ddcl|header=cuchar|since=c++11|
std::size_t c32rtomb( char* s, char32_t c32, std::mbstate_t* ps);
+
std::size_t c32rtomb( char* s, char32_t c32, std::mbstate_t* ps );
 
}}
 
}}
  
Converts a 32-bit wide character to its narrow multibyte representation.
+
Converts a UTF-32 character to its narrow multibyte representation.
  
If {{tt|s}} is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation of {{tt|c32}} (including any shift sequences), and stores the multibyte character representation in the character array whose first element is pointed to by {{tt|s}}. At most {{c|MB_CUR_MAX}} bytes can be written by this function.
+
If {{c|s}} is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation of {{c|c32}} (including any shift sequences, and taking into account the current multibyte conversion state {{c|*ps}}), and stores the multibyte character representation in the character array whose first element is pointed to by {{c|s}}, updating {{c|*ps}} as necessary. At most {{c|MB_CUR_MAX}} bytes can be written by this function.
  
If {{tt|s}} is a null pointer, the call is equivalent to {{c|std::c32rtomb(buf, U'\0', ps)}} for some internal buffer {{tt|buf}}.
+
If {{c|s}} is a null pointer, the call is equivalent to {{c|std::c32rtomb(buf, U'\0', ps)}} for some internal buffer {{tt|buf}}.
  
If c32 is the null wide character {{c|U'\0'}}, a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter {{c|*ps}} is updated to represent the initial shift state.
+
If {{c|c32}} is the null wide character {{c|U'\0'}}, a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter {{c|*ps}} is updated to represent the initial shift state.
  
If the macro {{c|__STDC_UTF_32__}} is defined, the 32-bit encoding used by this function is UTF-32, otherwise it is implementation-defined. In any case, the multibyte encoding used by this function is specified by the currently active C locale.
+
The multibyte encoding used by this function is specified by the currently active C locale.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | s | pointer to narrow character array where the multibyte character will be stored}}
+
{{par|s|pointer to narrow character array where the multibyte character will be stored}}
{{param list item | c32 | the 32-bit character to convert}}
+
{{par|c32|the 32-bit character to convert}}
{{param list item | ps | pointer to the conversion state object used when interpreting the multibyte string }}
+
{{par|ps|pointer to the conversion state object used when interpreting the multibyte string}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by {{tt|s}}. This value may be {{c|0}}, e.g. when processing the first {{c|char32_t}} in multi-{{c|char32_t}}-character sequence (does not occur in UTF-32).
+
On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by {{c|s}}. This value may be {{c|0}}, e.g. when processing the first {{c|char32_t}} in multi-{{c|char32_t}}-character sequence (does not occur in UTF-32).
  
On failure (if {{c|c32}} is not a valid 32-bit character), returns {{c|static_cast<std::size_t>(-1)}}, stores {{c|EILSEQ}} in {{c|errno}}, and leaves {{c|*ps}} in unspecified state.
+
On failure (if {{c|c32}} is not a valid 32-bit character), returns {{c|-1}}, stores {{lc|EILSEQ}} in {{lc|errno}}, and leaves {{c|*ps}} in unspecified state.
 +
 
 +
===Example===
 +
{{example|
 +
|code=
 +
#include <climits>
 +
#include <clocale>
 +
#include <cuchar>
 +
#include <iomanip>
 +
#include <iostream>
 +
#include <string_view>
 +
 
 +
int main()
 +
{
 +
    std::setlocale(LC_ALL, "en_US.utf8");
 +
    std::u32string_view strv = U"zß水🍌"; // or z\u00df\u6c34\U0001F34C
 +
    std::cout << "Processing " << strv.size() << " UTF-32 code units: [ ";
 +
    for (char32_t c : strv)
 +
        std::cout << std::showbase << std::hex << static_cast<int>(c) << ' ';
 +
    std::cout << "]\n";
 +
 
 +
    std::mbstate_t state{};
 +
    char out[MB_LEN_MAX]{};
 +
    for (char32_t c : strv)
 +
    {
 +
        std::size_t rc = std::c32rtomb(out, c, &state);
 +
        std::cout << static_cast<int>(c) << " converted to [ ";
 +
        if (rc != (std::size_t) - 1)
 +
            for (unsigned char c8 : std::string_view{out, rc})
 +
                std::cout << +c8 << ' ';
 +
        std::cout << "]\n";
 +
    }
 +
}
 +
|output=
 +
Processing 4 UTF-32 code units: [ 0x7a 0xdf 0x6c34 0x1f34c ]
 +
0x7a converted to [ 0x7a ]
 +
0xdf converted to [ 0xc3 0x9f ]
 +
0x6c34 converted to [ 0xe6 0xb0 0xb4 ]
 +
0x1f34c converted to [ 0xf0 0x9f 0x8d 0x8c ]
 +
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/string/multibyte/dcl list mbrtoc32}}
+
{{dsc inc|cpp/string/multibyte/dsc mbrtoc32}}
{{dcl list template | cpp/locale/codecvt/dcl list do_out | mem=std::codecvt<char32_t, char, std::mbstate_t>}}
+
{{dsc inc|cpp/locale/codecvt/dsc do_out|mem=std::codecvt<char32_t, char, std::mbstate_t>}}
{{dcl list end}}
+
{{dsc see c|c/string/multibyte/c32rtomb}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 02:00, 9 June 2023

Defined in header <cuchar>
std::size_t c32rtomb( char* s, char32_t c32, std::mbstate_t* ps );
(since C++11)

Converts a UTF-32 character to its narrow multibyte representation.

If s is not a null pointer, the function determines the number of bytes necessary to store the multibyte character representation of c32 (including any shift sequences, and taking into account the current multibyte conversion state *ps), and stores the multibyte character representation in the character array whose first element is pointed to by s, updating *ps as necessary. At most MB_CUR_MAX bytes can be written by this function.

If s is a null pointer, the call is equivalent to std::c32rtomb(buf, U'\0', ps) for some internal buffer buf.

If c32 is the null wide character U'\0', a null byte is stored, preceded by any shift sequence necessary to restore the initial shift state and the conversion state parameter *ps is updated to represent the initial shift state.

The multibyte encoding used by this function is specified by the currently active C locale.

Contents

[edit] Parameters

s - pointer to narrow character array where the multibyte character will be stored
c32 - the 32-bit character to convert
ps - pointer to the conversion state object used when interpreting the multibyte string

[edit] Return value

On success, returns the number of bytes (including any shift sequences) written to the character array whose first element is pointed to by s. This value may be 0, e.g. when processing the first char32_t in multi-char32_t-character sequence (does not occur in UTF-32).

On failure (if c32 is not a valid 32-bit character), returns -1, stores EILSEQ in errno, and leaves *ps in unspecified state.

[edit] Example

#include <climits>
#include <clocale>
#include <cuchar>
#include <iomanip>
#include <iostream>
#include <string_view>
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    std::u32string_view strv = U"zß水🍌"; // or z\u00df\u6c34\U0001F34C
    std::cout << "Processing " << strv.size() << " UTF-32 code units: [ ";
    for (char32_t c : strv)
        std::cout << std::showbase << std::hex << static_cast<int>(c) << ' ';
    std::cout << "]\n";
 
    std::mbstate_t state{};
    char out[MB_LEN_MAX]{};
    for (char32_t c : strv)
    {
        std::size_t rc = std::c32rtomb(out, c, &state);
        std::cout << static_cast<int>(c) << " converted to [ ";
        if (rc != (std::size_t) - 1)
            for (unsigned char c8 : std::string_view{out, rc})
                std::cout << +c8 << ' ';
        std::cout << "]\n";
    }
}

Output:

Processing 4 UTF-32 code units: [ 0x7a 0xdf 0x6c34 0x1f34c ]
0x7a converted to [ 0x7a ]
0xdf converted to [ 0xc3 0x9f ]
0x6c34 converted to [ 0xe6 0xb0 0xb4 ]
0x1f34c converted to [ 0xf0 0x9f 0x8d 0x8c ]

[edit] See also

(C++11)
converts a narrow multibyte character to UTF-32 encoding
(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 c32rtomb