mbrtoc32() in C/C++ with Examples
The mbrtoc32() is a built-in function in C/C++ which converts a narrow multibyte character to a 32 bit character representation. It is defined within the uchar.h header file of C++.
Syntax:
size_t mbrtoc32( char32_t* pc32, const char* s, size_t n, mbstate_t* ps);
Parameters: The function accepts four mandatory parameter which are described below:
- s : specifies to the multibyte character to convert.
- pc32 : specifies to the memory location to store the resulting 32 bit character.
- n : specifies the maximum number of bytes in s to convert.
- ps : specifies to an mbstate_t object used when interpreting the multibyte string.
Return Value: The function returns five values as follows:
- 0 if the converted character is a null character.
- the number of bytes (at most n) of the multibyte character that was successfully converted to 32 bit character.
- -3 if the next char16_t from a multi-char32_t character (e.g. a surrogate pair) has now been written to *pc32. 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. In this case nothing is written to *pc32.
- -1 if encoding error occurs. In this case nothing is written to *pc32, errno is set to EILSEQ and the value of *ps is unspecified.
Below programs illustrate the above function.
Program 1:
// C++ program to illustrate the // mbrtoc32() function #include <cstdio> #include <cstdlib> #include <iostream> #include <uchar.h> #include <wchar.h> using namespace std; int main(void) { char32_t pc32; char s[] = "S"; mbstate_t ps{}; int length; // initializing the function length = mbrtoc32(&pc32;, s, MB_CUR_MAX, &ps;); if (length < 0) { perror("mbrtoc32() fails to convert"); exit(-1); } cout << "Multibyte string = " << s << endl; cout << "Length = " << length << endl; printf("32-bit character = 0x%04hx\n", pc32); return 0; } |
Multibyte string = S Length = 1 32-bit character = 0x0053
Program 2:
// C++ program to illustrate the // mbrtoc32() function #include <cstdio> #include <cstdlib> #include <iostream> #include <uchar.h> #include <wchar.h> using namespace std; int main(void) { char32_t pc32; char s[] = "S"; mbstate_t ps{}; int length; // initializing the function length = mbrtoc32(&pc32;, s, MB_CUR_MAX, &ps;); if (length < 0) { perror("mbrtoc32() fails to convert"); exit(-1); } cout << "Multibyte string = " << s << endl; cout << "Length = " << length << endl; printf("32-bit character = 0x%08hx\n", pc32); return 0; } |
Multibyte string = S Length = 1 32-bit character = 0x00000053
Recommended Posts:
- SDL library in C/C++ with examples
- set_symmetric_difference in C++ with Examples
- ratio_equal() in C++ with examples
- Manipulators in C++ with Examples
- scanf("%[^\n]s", str) Vs gets(str) in C with Examples
- std::allocator() in C++ with Examples
- Pointers in C/C++ with Examples
- mbrtoc16() in C/C++ with Examples
- std::mismatch() with examples in C++
- memset() in C with examples
- cauchy_distribution a() in C++ with Examples
- negative_binomial_distribution in C++ with Examples
- ios bad() function in C++ with Examples
- feclearexcept in C++ with Examples
- C/C++ For loop with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



