Vikram
Vikram

Reputation: 76

Not able to reading utf-8 file in Qt

I am reading a file with utf-8 encoding. Below is my code.

QFile file("file.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
  return 1;
}
QTextStream ts(&file);
ts.setCodec( "UTF-8" );
QString str = ts.readLine();    
qDebug() << str << str.toUtf8().toHex();

output: "??" "f09d9c86"

file contain only one character: 𝜆 i.e.Lambda unicode value of Lambda is 3bb and its utf equivalent is cebb then why am i getting output as f09d9c86

when i hardcode the string in code i get correct result.

QString str = QString::fromWCharArray(L"𝜆");
qDebug() << str << str.toUtf8().toHex();

am I doing something wrong? please help.

Upvotes: 1

Views: 580

Answers (1)

wRAR
wRAR

Reputation: 25693

The UTF-8 representation of 𝜆 (U+1D706 MATHEMATICAL ITALIC SMALL LAMDA) is f0 9d 9c 86.

Upvotes: 4

Related Questions