Write catalog title here
QString
Qt's QString class provides a very convenient interface for string manipulation
//QString::arg()用法
QString str = QString("%1 %2 %3").arg(1).arg(2.0).arg("hello");
%1, %2, %3 as placeholders will be replaced by the content in the arg() function that follows. For example, %1 will be replaced with 1, %2 will be replaced with 2.0, and %3 will be replaced Into "hello".
1 2 hello
QByteArray
Qt's QByteArray class provides a very convenient interface for byte stream operations
The difference between QString and QByteArray
QString is specially used to process strings . In addition to processing ASCII coded characters, it also includes the encoding of various languages. By default, QString will store and process all data as utf-8 encoding .
QByteArray is only used to process data and can only process ASCII encoded characters. Other complex encodings (such as utf-8 encoding) are treated directly as a byte stream.
QString str("小马哥");
QByteArray byte("小马哥");
qDebug() << "str:" << str << "byte:" << byte << endl;
输出结果:str: "小马哥" byte: "\xE5\xB0\x8F\xE9\xA9\xAC\xE5\x93\xA5",
str retains the encoding format and can output Chinese,
but QByteArray only treats "小马哥" as ordinary byte data to process
utf-8 encoding, a Chinese character occupies three bytes
QString转QByteArray
QString str("123abc小马哥");
QByteArray byte1 = str.toLatin1(); //按照ASCII编码转换,无法转换中文
QByteArray byte2 = str.toUtf8(); //按照Utf-8编码转换,可以转换中文
qDebug() << "byte1:" << byte1 << "byte2:" << byte2;
Output result:
byte1: "123abc???"
byte2: "123abc\xE5\xB0\x8F\xE9\xA9\xAC\xE5\x93\xA5"
QByteArray转QString
QByteArray byte("123abc小马哥");
QString str(byte);//QString str = byte;
qDebug() << "byte:" << byte << "str:" << str;
Output result:
byte: "123abc\xE5\xB0\x8F\xE9\xA9\xAC\xE5\x93\xA5"
str: "123abc小马哥"
QString and vector conversion
vector array (assign value) to string
double can be directly given to utf-8
//直接转
QString value_i;
for (int i = 0; i < pso->g_best.size(); ++i){
value_i.append(QString("%1").arg(pso->g_best.at(i))).append(QString("\n"));}
String (assign value) to vector array
utf-8 needs to be converted to double
//toDouble()
while (!file.atEnd()){
QString line = file.readLine().trimmed();
one_array.push_back(line.toDouble());
}