Background:
Recently, I am developing PC software related to Qt serial port. Therefore, many IO operations are involved, and IO receiving is often stored in QByteArray. Therefore, the conversion between QByteArray and QString is inevitable.
There are already a lot of conversions on the Internet. Here I will focus on the two methods in my practical application and the method of converting to a hexadecimal number.
The first method of QByteArray to QString:
/*************************************** ***********************************/
QByteArray byteArrayTempInfo;
****
ByteArrayTempInfo. toHex ();
/*************************************** ***********************************/
This method is convenient, and the data in AByteArray is converted into a QString at once.
The second method of QByteArray to QString:
/*************************************** ***********************************/
QByteArray byteArrayTempInfo;
****
Unsigned char iTemp = byteArrayTempInfo. at (I );
QString str = QString: number (iTemp & 0xff, 16 );
If (iTemp <10) str. insert (0, "0"); // note
/*************************************** ***********************************/
This method is not as easy as the first method. Pay attention to the use of 0xff. At the same time, you must note that when some data is smaller than 10, you must add 0.