Qt532的QString输出各种编码的16进制数组(测试代码)
环境:Win7x64、Qt5.3.2 MSVC2010 OpenGL、vs2010
1、只测试了 4种:Utf8、Latin1、Local8Bit、unicode
2、测试代码:(这个是 "MainWindow.cpp",文件的编码方式是 "UTF8 + BOM")
#include "MainWindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } #include <QTextCodec> const char* JDP = "接地牌"; QString g_JDP = QString::null; const char* KG_0 = "开关_0"; const char* KG_1 = "#开关_1"; const char* ZD_0 = "#闸刀_0"; const char* ZD_1 = "#闸刀_1"; const char* DD_0 = "#地刀_0"; const char* DD_1 = "#地刀_1"; const char* PB = "配变"; const char* DLSRDQ_0 = "跌落式熔断器_0"; const char* DLSRDQ_1 = "#跌落式熔断器_1"; const char* TJX = "T接线"; const char* TXLXMC = "图形类型名称"; //const char* YZSJB = "有载三卷变"; const char* UnitClassify_YZLJB = "有载两卷变"; const char* UnitClassify_YZSJB = "有载三卷变"; const char* UnitClassify_SCKG = "手车开关"; const char* UnitClassify_TextRect = "文本框"; // *** QString g_KG_0 = QString::null; QString g_KG_1 = QString::null; QString g_ZD_0 = QString::null; QString g_ZD_1 = QString::null; QString g_DD_0 = QString::null; QString g_DD_1 = QString::null; QString g_PB = QString::null; QString g_DLSRDQ_0 = QString::null; QString g_DLSRDQ_1 = QString::null; QString g_TJX = QString::null; QString g_TXLXMC = QString::null; //QString g_YZSJB = QString::null; QString g_UnitClassify_YZLJB = QString::null; QString g_UnitClassify_YZSJB = QString::null; QString g_UnitClassify_SCKG = QString::null; QString g_UnitClassify_TextRect = QString::null; const char* Test_Z01 = "Unicode编码的字符串(ZC: 双字节)"; QString g_Test_Z01 = QString::null; void StringCHS_init() { QTextCodec *pCodec = QTextCodec::codecForName("GBK"); g_JDP = pCodec->toUnicode(JDP); g_KG_0 = pCodec->toUnicode(KG_0); g_KG_1 = pCodec->toUnicode(KG_1); g_ZD_0 = pCodec->toUnicode(ZD_0); g_ZD_1 = pCodec->toUnicode(ZD_1); g_DD_0 = pCodec->toUnicode(DD_0); g_DD_1 = pCodec->toUnicode(DD_1); g_PB = pCodec->toUnicode(PB); g_DLSRDQ_0 = pCodec->toUnicode(DLSRDQ_0); g_DLSRDQ_1 = pCodec->toUnicode(DLSRDQ_1); g_TJX = pCodec->toUnicode(TJX); g_TXLXMC = pCodec->toUnicode(TXLXMC); //g_YZSJB = pCodec->toUnicode(YZSJB); g_UnitClassify_YZLJB = pCodec->toUnicode(UnitClassify_YZLJB); g_UnitClassify_YZSJB = pCodec->toUnicode(UnitClassify_YZSJB); g_UnitClassify_SCKG = pCodec->toUnicode(UnitClassify_SCKG); g_UnitClassify_TextRect = pCodec->toUnicode(UnitClassify_TextRect); g_Test_Z01 = pCodec->toUnicode(Test_Z01); } #include <QDebug> void MainWindow::on_pbtn2Utf8_clicked() { StringCHS_init(); QString strPrint(""); QByteArray ba = g_Test_Z01.toUtf8(); for (int i=0; i<ba.length(); i++) { char c = ba.at(i); strPrint += QString::number( ((int)c)&0xFF, 16 ) + " "; } qDebug() << "Utf8 :"; qDebug() << strPrint; qDebug() << ""; strPrint.clear(); ba = g_Test_Z01.toLatin1(); for (int i=0; i<ba.length(); i++) { char c = ba.at(i); strPrint += QString::number( ((int)c)&0xFF, 16 ) + " "; } qDebug() << "Latin1 :"; qDebug() << strPrint; qDebug() << ""; strPrint.clear(); ba = g_Test_Z01.toLocal8Bit(); for (int i=0; i<ba.length(); i++) { char c = ba.at(i); strPrint += QString::number( ((int)c)&0xFF, 16 ) + " "; } qDebug() << "Local8Bit :"; qDebug() << strPrint; qDebug() << ""; strPrint.clear(); for (int i=0; i<g_Test_Z01.length(); i++) { QChar qc = g_Test_Z01.at(i); ushort us = qc.unicode(); strPrint += QString::number( us, 16 ) + " "; } qDebug() << "unicode :"; qDebug() << strPrint; }
3、
4、
5、