mbrtoc16
Defined in header <uchar.h>
|
||
(since C11) | ||
Converts a narrow multibyte character to 16-bit character representation (typically, UTF-16).
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). If the function determines that the next multibyte character in s
is complete and valid, converts it to the corresponding 16-bit character and stores it in *pc16 (if pc16
is not null).
If the multibyte character in *s
corresponds to a multi-char16_t sequence (e.g. a surrogate pair in UTF-16), then after the first call to this function, *ps
is updated in such a way that the next call to mbrtoc16
will write out the additional char16_t, without considering *s
.
If s
is a null pointer, the values of n
and pc16
are ignored and the call is equivalent to mbrtoc16(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_16__ is defined, the 16-bit encoding used by this function is UTF-16, otherwise it is implementation-defined.
Contents |
[edit] Parameters
pc16 | - | pointer to the location where the resulting 16-bit 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 |
[edit] Return value
The first of the following that applies:
- 0 if the character converted from
s
(and stored in *pc16 if non-null) was the null character - the number of bytes [1...n] of the multibyte character successfully converted from
s
- -3 if the next char16_t from a multi-char16_t character (e.g. a surrogate pair) has now been written to *pc16. No bytes are processed from the input in this case.
- -2 if the next
n
bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to *pc16. - -1 if encoding error occurs. Nothing is written to
*pc16
, the value EILSEQ is stored in errno and the value if *ps is unspecified.
[edit] Example
#include <stdio.h> #include <locale.h> #include <string.h> #include <uchar.h> mbstate_t state; int main(void) { setlocale(LC_ALL, "en_US.utf8"); char *str = u8"z\u00df\u6c34\U0001F34C"; // or u8"zß水🍌" printf("Processing %zu bytes: [ ", strlen(str)); for(char* p = str; *p; ++p) printf("%#x ", +(unsigned char)*p); puts("]"); char16_t c16; char *ptr = str, *end = str + strlen(str); int rc; while(rc = mbrtoc16(&c16, ptr, end - ptr, &state)) { printf("Next UTF-16 char: %#x obtained from ", c16); if(rc == -3) puts("earlier surrogate pair"); else if(rc > 0) { printf("%d bytes [ ", rc); for(int n = 0; n < rc; ++n) printf("%#x ", +(unsigned char)ptr[n]); puts("]"); ptr += rc; } } }
Output:
Processing 10 bytes: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c ] Next UTF-16 char: 0x7a obtained from 1 bytes [ 0x7a ] Next UTF-16 char: 0xdf obtained from 2 bytes [ 0xc3 0x9f ] Next UTF-16 char: 0x6c34 obtained from 3 bytes [ 0xe6 0xb0 0xb4 ] Next UTF-16 char: 0xd83c obtained from 4 bytes [ 0xf0 0x9f 0x8d 0x8c ] Next UTF-16 char: 0xdf4c obtained from earlier surrogate pair
[edit] References
- C11 standard (ISO/IEC 9899:2011):
- 7.28.1.1 The mbrtoc16 function (p: 398-399)
[edit] See also
(C11) |
convert a 16-bit wide character to narrow multibyte string (function) |
C++ documentation for mbrtoc16
|