C Standard Libraries C++

wcrtomb()

Declaration

size_t wcrtomb(char* cpMBChar, wchar_t wWideChar, mbstate_t* qpMBState);

Description

This function converts the wide-character "wWideChar" to a multibyte character using the multibyte state pointed to by "qpMBState" and stores it at "cpMBChar." If the function is successful, it returns the number of bytes required to store the multibyte character. Otherwise, it returns -1 to indicate an error.

Example

#include <cwchar>
#include <cstdlib>
#include <cstdio>

int main()
{
    const wchar_t kwaWideString[] = L"XoaX.net";
    char* cpConverted = new char[MB_CUR_MAX];

    // Convert the first character of the wide string
    size_t uiByteReq = wcrtomb(cpConverted, kwaWideString[0], NULL);
    wprintf(L"%d bytes required.\n", uiByteReq);
    wprintf(L"Original: %wc\n", kwaWideString[0]);
    printf("Converted: %c\n", cpConverted[0]);

    delete [] cpConverted;
    // Keep the window open until "Enter" is pressed
    getwchar();
    return 0;
}

Output

wcrtomb() Output
 

© 2007–2022 XoaX.net LLC. All rights reserved.