1、int转QString
quint8转QString
1 quint8 k = 77; 2 QString str_1; 3 str_1 = QString::number(k, 10); 4 qDebug() << str_1; // "77" 5 QString str_2 = QString("%1").arg(k); 6 qDebug() << str_2; // "77"
2、quint8转QByteArray
1 QByteArray all; 2 quint8 head = 0XFF; 3 all.append(head); 4 qDebug() << all; //"\xFF"
3、quint8转QByteArray再转quint8
1 QByteArray all; 2 quint8 head = 65; 3 all.append(head); 4 qDebug() << all; //"A" // 可以看出是ascii码的值,65对应A 5 QByteArray temp = all.mid(0, 1); 6 qDebug() << temp; //"A" 7 char* data = temp.data(); 8 qDebug() << data; //A 9 quint8 i = (quint8) data[0]; 10 qDebug() << i; //65
4、QByteArray转char*
1 QByteArray b("123"); 2 qDebug() << b; //"123" 3 char* d = b.data(); 4 qDebug() << d; //123
5、quint8转QByteArray(感觉QByteArray.toInt()等方法很不好用。。)
1 quint8 q = 65; 2 QByteArray b; 3 b.setNum(q, 10); 4 qDebug() << b; //"65" 5 qDebug() << "int:" << b.toInt() << " hex:" << b.toHex(); //int: 65 hex: "3635" 6 // int转成功了,但是hex...额,没有看懂
6、QString转char*
1 QString str("123"); 2 std::string temp = str.toStdString(); 3 const char* constChar = temp.c_str(); 4 char* result = new char[strlen(constChar) + 1]; 5 strcpy(result, constChar); 6 qDebug() << result; //123
7、quint16转char*
1 quint16 code = 9299; 2 char buff[3]; 3 memcpy(buff,&code,sizeof(quint16)); 4 qDebug() << buff; //S$
8、char*转quint16
1 // buff为上一点中的数据 2 quint16 res = *((quint16*)buff); 3 qDebug() << res; //9299
9、quint16转QByteArray再转quint16
1 QByteArray q; 2 quint16 code = 9299; 3 char buff[3]; 4 memcpy(buff,&code,sizeof(quint16));//quint16转char* 5 qDebug() << buff; //S$ 6 7 q.append(buff); //char*转为QByteArray 8 char *temp = q.data(); // QByteArray转char* 9 quint16 res = *((quint16*)temp);// char*转quint16 10 qDebug() << "size:" << q.length() << " byte:" << q << " res:" << res;//size: 2 byte: "S$" res: 9299
标签:类型转换,常用,QByteArray,Qt,quint16,char,QString,qDebug,quint8 From: https://www.cnblogs.com/ybqjymy/p/18066884