2

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.

3 Answers3

3

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,

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
2

You could try wcstombs / mbstowcs.

sep
  • 3,409
  • 4
  • 29
  • 32
1

You can also use CT2A and pass CP_UTF8 as the code page, e.g.:

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.
Rob
  • 76,700
  • 56
  • 158
  • 197