C Standard Libraries C++

mbstowcs()

Declaration

size_t mbstowcs(wchar_t* wpString,
                const char* kcpMultibyteString,
                size_t qCount);

Description

Converts a string of mutlibyte characters to a string of wide characters. The arguments "wpString," "kcpMultibyteString," and "qCount" are the converted wide character string, the multibyte string to be converted, and the number of multibyte characters to convert, respectively. If the conversion was successful, the number of multibyte characters converted is returned. If "wpString" is NULL, the return value is required size of the destination string. If an invalid multibyte character is encountered, the function returns –1.

Example

#include <iostream>
#include <cstdlib>

int main() {
    using namespace std;

    wchar_t waWideChar[]        = L"XoaX.net";
    char* cpMultibyteString     = new char[9*MB_CUR_MAX];
    int iConvertLength = (int)wcstombs(cpMultibyteString, waWideChar, 9*MB_CUR_MAX);

    // Convert to a multibyte string
    cpMultibyteString[iConvertLength] = 0;
    cout << "Conversion Length = " << iConvertLength << endl;
    cout << "Conversion to Multibyte= " << cpMultibyteString << endl;

    // Convert back to a wide string
    wchar_t waConvWideChar[30];
    int iConvertedBack = (int)mbstowcs(waConvWideChar, cpMultibyteString, iConvertLength);
    cout << "Conversion Back Length = " << iConvertedBack << endl;
    cout << "Value of First Wide Back = " << waConvWideChar[0] << endl;
}

Output

mbstowcs() Output
 

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