Search code examples
c++mfcchargoogle-contacts-apimultibyte

Visual Studio multibyte chars to single bytes


Is there a simple way to convert multibyte UTF8 data (from Google Contacts API via https://www.google.com/m8/feeds/) to single bytes? I know the extended ASCII set is non-standard but, for example, my program which will display the info in an MFC CListBox is quite happy to show 'E acute' as 0xE9. I only need it to cope with a few similar European symbols. I've discovered I can convert everything with MultiByteToWideChar() but don't want to have to change lots of functions to accept wide characters if possible. Thanks.


Solution

  • If you need to convert char * from UTF8 to ANSI, try the following function:

    // change encoding from UTF8 to ANSI
    char* change_encoding_from_UTF8_to_ANSI(char* szU8)
    { 
        int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);
        wchar_t* wszString = new wchar_t[wcsLen + 1];
        ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);
        wszString[wcsLen] = '\0';
    
        int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
        char* szAnsi = new char[ansiLen + 1];
        ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
        szAnsi[ansiLen] = '\0';
    
        delete []wszString;
        return szAnsi;
    }