简体   繁体   中英

Converting MBCS stream to UTF-8 and vice versa in C++

I'm using Visual C++ (VS2005) and compiling the project in Multibyte Character Set (MBCS). However, the program needs to communicate with a webapp (which is in utf-8) via XMLRPC. So I'm thinking maybe I can use MBCS internally and convert the strings to utf-8 before sending them to the xmlrpc module and converting them back to MBCS after receiving from the webapi.

I'm wondering what's the best way to convert between MBCS and UTF-8 in VC++?

Thanks all.

Call MultiByteToWideChar to convert your string to unicode followed by a call to WideCharToMultiByte to convert the unicode to UTF-8. Reverse the process to go the other way,

您可以尝试wcstombs / mbstowcs

You can also use CT2A and pass CP_UTF8 as the code page, eg:

CT2A pszUTF8(_T("My DBCS string"), CP_UTF8);
// pszUTF8.m_psz contains the UTF8 string.

To go back again:

CA2T pszT(_T("My UTF8 string"), CP_UTF8);
// pszT.m_psz contains the TCHAR string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM