W3cubDocs

/C

mbrtoc32

Defined in header <uchar.h>
size_t mbrtoc32( char32_t restrict * pc32, const char * restrict s,
                 size_t n, mbstate_t * restrict ps );
(since C11)

Converts a single code point from its narrow multibyte character representation to its variable-length 32-bit wide character representation (but typically, UTF-32).

If s is not a null pointer, inspects at most n bytes of the multibyte character string, beginning with the byte pointed to by s to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences, and taking into account the current multibyte conversion state *ps). If the function determines that the next multibyte character in s is complete and valid, converts it to the corresponding 32-bit wide character and stores it in *pc32 (if pc32 is not null).

If the multibyte character in *s corresponds to a multi-char32_t sequence (not possible with UTF-32), then after the first call to this function, *ps is updated in such a way that the next calls to mbrtoc32 will write out the additional char32_t, without considering *s.

If s is a null pointer, the values of n and pc32 are ignored and the call is equivalent to mbrtoc32(NULL, "", 1, ps).

If the wide character produced is the null character, the conversion state *ps represents the initial shift state.

If the macro __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 character encoding used by this function is specified by the currently active C locale.

Parameters

pc32 - pointer to the location where the resulting 32-bit wide character will be written
s - pointer to the multibyte character string used as input
n - limit on the number of bytes in s that can be examined
ps - pointer to the conversion state object used when interpreting the multibyte string

Return value

The first of the following that applies:

  • ​0​ if the character converted from s (and stored in *pc32 if non-null) was the null character
  • the number of bytes [1...n] of the multibyte character successfully converted from s
  • (size_t)-3 if the next char32_t from a multi-char32_t character has now been written to *pc32. No bytes are processed from the input in this case.
  • (size_t)-2 if the next n bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to *pc32.
  • (size_t)-1 if encoding error occurs. Nothing is written to *pc32, the value EILSEQ is stored in errno and the value of *ps is unspecified.

Example

#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <uchar.h>
#include <assert.h>
 
mbstate_t state;
int main(void)
{
    setlocale(LC_ALL, "en_US.utf8");
    char in[] = u8"zß水🍌"; // or "z\u00df\u6c34\U0001F34C"
    size_t in_sz = sizeof in / sizeof *in;
 
    printf("Processing %zu UTF-8 code units: [ ", in_sz);
    for(size_t n = 0; n < in_sz; ++n) printf("%#x ", (unsigned char)in[n]);
    puts("]");
 
    char32_t out[in_sz];
    char *p_in = in, *end = in + in_sz;
    char32_t *p_out = out;
    size_t rc;
    while((rc = mbrtoc32(p_out, p_in, end - p_in, &state)))
    {
        assert(rc != (size_t)-3); // no surrogate pairs in UTF-32
        if(rc == (size_t)-1) break; // invalid input
        if(rc == (size_t)-2) break; // truncated input
        p_in += rc;
        p_out += 1;
    }
 
    size_t out_sz = p_out - out + 1;
    printf("into %zu UTF-32 code units: [ ", out_sz);
    for(size_t x = 0; x < out_sz; ++x) printf("%#x ", out[x]);
    puts("]");
}

Output:

Processing 11 UTF-8 code units: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c 0 ]
into 5 UTF-32 code units: [ 0x7a 0xdf 0x6c34 0x1f34c 0 ]

References

  • C11 standard (ISO/IEC 9899:2011):
    • 7.28.1.3 The mbrtoc32 function (p: 400-401)

See also

(C11)
converts a 32-bit wide character to narrow multibyte string
(function)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/string/multibyte/mbrtoc32