Conversion between QByteArray and QString of hexadecimal data in Qt

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.