[QT] Mutual conversion of QByteArray, QString, char* (text form, binary, hexadecimal)

1. Mutual conversion between QByteArray and QString

1.1 Conversion of QByteArray text form

1.1.1 QByteArray to QString

QByteArray byte = "ccc";
QString str = QString(byte);  //直接使用QString构造

1.1.2 QString to QByteArray

QString str = "aaa";
QByteArray byte = str.toLatin1();
QByteArray byte = toLocal8Bit();
QByteArray byte = toUtf8();
  1. toLatin1 : ISO-8859-1 encoding is a single-byte encoding, backward compatible with ASCII, and its encoding range is 0x00-0xFF
  2. toLocal8Bit : The gb18030 character set is compatible with the gbk character set, and a text is represented by two bytes. The windows system may use one of these two
  3. toUtf8 : The utf8 character set represents a Chinese character with 2 or more bytes. In fact, the specific value has a great correlation with unicode

1.2 Conversion of QByteArray non-text form (binary, hexadecimal)

QString can only be constructed when the data is text, and cannot be directly constructed for data in non-text form (binary or hexadecimal)

1.2.1 QByteArray to QString

Create a new notepad and name it 1.txt, and write data in it casually, use ZIP to compress it into 1.zip, and the zip compressed file is in hexadecimal data format.

QFile file("C:\\Users\\WL\\Desktop\\1.zip");  //压缩文件的路径,路径的\要使用\\进行转义,或者使用/
//QFile file("C:/Users/WL/Desktop/1.zip")
if(!file.open(QIODevice::ReadOnly)){
    
    
	qDebug() << "打开文件失败";
	return;  //只读打开文件失败则返回
}
QByteArray byte = file.readAll();  //读取数据

//QString str = QString(byte);  //错误,16进制的数据不能直接使用QString构造,否则输出为空

//以下六种方法任意一种都可以
//先使用toHex() 或者 toBase64()
//再使用fromLatin1 或 fromLocal8Bit 或 fromUtf8 转化为QString
QString str1 = QString::fromLatin1(byte.toHex());

QString str2 = QString::fromLatin1(byte.toBase64());

QString str3 = QString::fromLocal8Bit(byte.toHex());

QString str4 = QString::fromLocal8Bit(byte.toBase64());

QString str5 = QString::fromUtf8(byte.toHex());

QString str6 = QString::fromUtf8(byte.toBase64());

1.2.2 QString to QByteArray

//针对上面6个QString的str1,str2,str3,str4,str5,str6 转化为 QByteArray
QByteArray byte1 = QByteArray::fromHex(str1.toLatin1);

QByteArray byte2 = QByteArray::fromBase64(str2.toLatin1);

QByteArray byte3= QByteArray::fromHex(str3.toLocal8Bit);

QByteArray byte4 = QByteArray::fromBase64(str4.toLocal8Bit);

QByteArray byte5 = QByteArray::fromHex(str5.toUtf8);

QByteArray byte6 = QByteArray::fromBase64(str6.toUtf8);

2. Mutual conversion between QByteArray and char *

2.1 QByteArray converted to char *

char *QByteArray::data()

data() returns a pointer to the data stored in the byte array. Pointers can be used to access and modify the bytes that make up the array.
The data ends with "\0", that is, for the "\0" terminator, the number of bytes in the returned string is size() + 1.

//QByteArray 转化为 char * 
QByteArray ba("Hello world");
char *ch = ba.data();  //ch是指针
qDebug() << *ch;  //*ch指向字符'H',输出:H
qDebug() << ch;  //输出:Hello world

Someone may ask, isn't ch a pointer variable? But why isn't the address printed?
Answer: Yes, ch must be a pointer variable, and it must also be an address. But qDebug() is to pass the string to be output as a parameter to the function.
In C language, the system will add \0 at the end of the string by default to end the string, so when printing out, as long as your print format is %s, the system knows that we want to output a string, and the system can also Determine the length of the string, so when you use %s to print the character pointer p, the string will be output. If you output in the address printing format, the system will print out the address for us. If you are the solution pointer (* p), then the system will take the content pointed to by the p pointer. At this time, it depends on what format you output, and print in whatever format.

2.1 convert char * to QByteArray

char *ch = "Hello world";
QByteArray ba = QByteArray(ch);  //直接使用QByteArray()构造
qDebug() << ba;  //输出:"Hello world"
qDebug().noquote() << ba;  //去除字符串的双引号打印,输出:Hello world

3. Mutual conversion between QString and char *

3.1 Convert QString to char *

3.1.1 Convert to QByteArray first, then convert to char *

QString str("Hello world");
//先转化为QByteArray,再转化为char *
char* ch = str.toLatin1().data();
//char* ch = str.toLocal8Bit().data();
//char* ch = str.toUtf8().data();
qDebug() << ch;  //输出:Hello world

3.1.2 First convert to the string type in the standard library, and then convert to char *

Note: c_str() returns const

QString str("Hello world");
std::string s = str.toStdString().c_str();
const char* ch = s.c_str();
qDebug() << ch;  //输出:Hello world

3.2 convert char * to QString

char *ch = "Hello world";
QString str(ch);  //直接使用QString()构造
qDebug() << str;  //输出:"Hello world"

Guess you like

Origin blog.csdn.net/WL0616/article/details/129615310