mbrtowc() in C++

mbrtowc(): This method is available in wchar.h

Syntax:

size_t mbrtowc (wchar_t* pwc, const char* pmb, size_t max, mbstate_t* ps)

This function takes four arguments as its parameter. It converts the multibyte sequence to wide-character The multibyte character pointed by pmb is converted to a value of type wchar_t and stored at the location pointed by pwc. The function returns the length in bytes of the multibyte character. The function uses (and updates) the shift state described by ps. If ps is a null pointer, the function uses its own internal shift state, which is altered as necessary only by calls to this function.

Parameters: Four parameters are required for this method.

pwc: Pointer to an object of type wchar_t.

pmb: Pointer to the first byte of a multibyte character.

max: Maximum number of bytes to read from pmb. The macro constant MB_CUR_MAX defines the maximum number of bytes that can form a multibyte character under the current locale settings.

ps: Pointer to a mbstate_t object that defines a conversion state.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{

    wchar_t dest;
    mbstate_t mbs;
    const char *str = "Hello World!";
    int max = sizeof(str);
    mbrlen(NULL0, &mbs);

    while (max > 0)
    {
        size_t length = mbrtowc(&deststrmax, &mbs);
        if ((length == 0) || (length > max))
            break;
        wprintf(L"%lc"dest);
        str += length;
        max -= length;
    }

    return 0;
}

Output:

Hello Wo


No comments:

Post a Comment