Sparsil information from the database (sqlite) I use the module on c ++ sqlite3. Data comes in type:

const unsigned char *id; id = sqlite3_colum_text(stmt, 0); 

Data comes but instead Cyrillic krakozyaby. How can I convert the whole thing into a normal encoding (for example, in UTF-8) and output it to a label? I work with QT4.

  • Well, first you need to figure out - you get a string there in the form of single-byte ASCII characters or, after all, Unicode code points and, if so, in what form - UTF8, UTF16 or UTF32. And then from the array of bytes, if necessary, you can make a string. But for this you have to look for the appropriate codec: stackoverflow.com/questions/14131127/qbytearray-to-qstring - gecube
  • And if you try QObject :: trUtf8 () - yrHeTaTeJlb

2 answers 2

Depending on how you opened the database, the default encoding will be UTF-8 (for sqlite3_open() and sqlite3_open_v2() ) or UTF-16 (for sqlite3_open16() ). But you can use sqlite3_colum_text16() - it returns UTF-16 anyway. Then you can do something like this:

 int column = 0; std::wstring str; int len = sqlite3_column_bytes(stmt, column); const wchar_t* c_str = static_cast<const wchar_t*>(sqlite3_column_text16(stmt, column)); if (c_str && len > 0) str.assign(c_str, len); else str = L""; 

    To get a string in utf-8, put it there (in the database) in utf-8. At a DB there all is transparent.