Open In App

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:

Return Value: The function returns five values as follows:

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;
}

Output:
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;
}

Output:
Multibyte string = S
Length = 1
32-bit character = 0x00000053

Article Tags :