I have been struggling with character conversion, please help.
I have a file in Chinese (GB2312), I need to read it and then print it on screen (in linux), I got garbage output on screen such as ��?��. I have tried for hours with different conversion. Nothing works!
Here is the code:
static QString toUnicode
( const QString
& str
) {
//return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toLocal8Bit(); // <- not working
//return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toUtf8(); // <- not working
return QTextCodec::codecForName( "GB2312" )->toUnicode
( str.
toLatin1() );
// <- not working }
void my_read_func()
{
settings.
setIniCodec( QTextCodec::codecForName( "GB2312" ) );
settings.beginGroup( "xxxx" );
{
foreach( const QString& key, settings.allKeys() ) {
QStringList strList
= settings.
value( key
).
toStringList();
qDebug() << key << "=" << strList; // <- garbage out
foreach( const QString& str, strList ) {
qDebug() << toUnicode( key ) << "=" << toUnicode( str ); // <- garbage out
}
}
}
settings.endGroup();
}
static QString toUnicode( const QString& str )
{
//return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toLocal8Bit(); // <- not working
//return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ).toUtf8(); // <- not working
return QTextCodec::codecForName( "GB2312" )->toUnicode( str.toLatin1() ); // <- not working
}
void my_read_func()
{
QSettings settings( filename, QSettings::IniFormat );
settings.setIniCodec( QTextCodec::codecForName( "GB2312" ) );
settings.beginGroup( "xxxx" );
{
foreach( const QString& key, settings.allKeys() ) {
QStringList strList = settings.value( key ).toStringList();
qDebug() << key << "=" << strList; // <- garbage out
foreach( const QString& str, strList ) {
qDebug() << toUnicode( key ) << "=" << toUnicode( str ); // <- garbage out
}
}
}
settings.endGroup();
}
To copy to clipboard, switch view to plain text mode
Bookmarks