QString、QByteArray、ASCII码、16进制等类型转换和编码转换

 

1、字符串转ASCII码

1 1 QString str = "abc123";
2 2 QByteArry data = str.toUtf8();    //输出data:61 62 63 31 32 3

 

2、ASCII码转字符串

1 QByteArray data = {61, 62, 63, 31, 32, 33};
2 QString str;
3 for(int i = 0; i < data .count(); ++i)
4 {
5     str.append(QChar(data .at(i)));
6 }
7 //输出str:abc123

 

3、16进制以字符串输出

1 QByteArray data= {0x45, 0x65, 0x75};
2 QString str = QString("%1").arg(data.toHex(":"));    //输出45:65:75

 

4、QString转为LPSTR,LPTSTR

(1)作为函数形参

1 QString str = tr("hello");
2 (LPSTR)str.toLocal8Bit().data();
3 (LPTSTR)str.utf16();

 

(2)直接赋值

1 LPSTR data = (LPSTR)"hello"2 LPTSTR info = (LPTSTR)"hello";

 

posted @ 2020-10-23 17:26  绿筱  阅读(8009)  评论(0编辑  收藏  举报