最經在研究AT指令接受短信,短信是unicode編碼,接受后需要根據系統的編碼方案進行相關的轉碼
比如接受到了一串字符4F60597D,它是“你好”的unicode編碼,一個unicode編碼占兩個字節,所有可以使用4個16進制數表示:4F60->你,597D->好。那我們怎么轉換了?
在QString中存放的是QChar,你可以把她當作ushort來看待。因為Qt本身的編碼方案就是unicode。
QTextCodec *codec = QTextCodec::codecForName("utf-8"); QString str = "0891683108500145F1240D91685143256178F0000831214281659423044F60597D"; QString t = str.mid(58); QStringList s; for(int i = 0;i < t.length();i += 4) { s.append(t.mid(i,4)); } QString t1; foreach (const QString &t, s) { t1.append(t.toUShort(0,16)); } QString re = codec->fromUnicode(t1); qDebug() << QObject::trUtf8(re.toLatin1().data());
先將要處理的字符串按4個一段分割然后轉化成ushort也就是QChar然后拼成一個QString,其實兩步可以化成一步做。
參考:http://blog.csdn.net/zhx6044/article/details/17656989