C Standard Libraries C++

mbtowc()

Declaration

int mbtowc(wchar_t* wpString, const char* kcpMultibyteString, size_t qMaxBytes);

Description

Converts a string of multibyte characters to a string of wide characters. The arguments "wpString," "kcpMultibyteString," and " qMaxBytes " are the converted wide character, the multibyte character to be converted, and the maximum number of bytes to convert, respectively. If the conversion was successful, the number of bytes converted is returned.

Example

#include <iostream>
#include <cstdlib>

int main() {
    using namespace std;

    wchar_t wWideChar       = L'X';
    char* cpMultibyteChar   = new char[MB_CUR_MAX + 1];
    int iConvertLength      = wctomb(cpMultibyteChar, wWideChar);
    // Null terminate the chars
    cpMultibyteChar[iConvertLength] = 0;

    // Convert to a multibyte char
    cout << "Conversion Length = " << iConvertLength << endl;
    cout << "Conversion = " << cpMultibyteChar << endl;

    // Convert back to a wide char
    wchar_t wConvWideChar;
    int iConvertedBack = mbtowc(&wConvWideChar, cpMultibyteChar, MB_CUR_MAX);
    cout << "Conversion Back Length = " << iConvertedBack << endl;
    cout << "Value of Conversion Back = " << wConvWideChar << endl;
}

Output

mbtowc() Output
 

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