简体   繁体   中英

Wrong encoding when getting a string from MySQL database with C++

I'm writing an MFC app with C++ in Visual Studio 2012. App connects to a MySQL database and shows every row to a List Box. Words are in Russian, database encoding is cp1251. I've set the same character set using this code:

if (!mysql_set_character_set(mysql, "cp1251")) {
    statusBox.SetWindowText((CString)"CP1251 is set for MYSQL.");
}

But it doesn't help at all. I display data using this code:

while ((row = mysql_fetch_row(result)) != NULL) {
    CString string = (CString)row[1];
    listBox.AddString(string);
}

This code also doesn't help:

mysql_query(mysql, "set names cp1251");

Please help. What should I do to display cyrillic correctly?

When crossing system boundaries that use different character encodings you have to convert between them. In this case, the MySQL database uses CP 1251 while Windows (and CString ) use UTF-16. The conversion might look like this:

#if !defined(_UNICODE)
    #error Unicode configuration required
#endif

CString CPtoUnicode( const char* CPString, UINT CodePage ) {
    CString retValue;
    // Retrieve required string length
    int len = MultiByteToWideChar( CodePage, 0,
                                   CPString, -1,
                                   NULL, 0 );
    if ( len == 0 ) {
        // Error -> return empty string
        return retValue;
    }

    // Allocate CString's internal buffer
    LPWSTR buffer = retValue.GetBuffer( len );
    // Do the conversion
    MultiByteToWideChar( CodePage, 0,
                         CPString, -1,
                         buffer, len );
    // Return control of the buffer back to the CString object
    retValue.ReleaseBuffer();
    return retValue;
}

This should be used as follows:

while ( ( row = mysql_fetch_row( result ) ) != NULL ) {
    CString string = CPtoUnicode( row[1], 1251 );
    listBox.AddString( string );
}

Alternatively, you could use CString s built-in conversion support, which requires to set the thread's locale to the source encoding (CP 1251) and use the conversion constructor.

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