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";

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM