// 环境: vs2010 + qt5.4.1
// 1.QString转char* (注意: 必须分两步写,不能写成str.toLocal8Bit.data())
QString str = "192.168.0.2";
QByteArray qByteArray = str.toLocal8Bit();//toLocal8Bit() toUtf8()
char* ch = qByteArray.data();
QString str2 = QString::fromLocal8Bit("中国人");//或者QStringLiteral("中国人");
QByteArray qByteArray2 = str2.toLocal8Bit();
char* ch2 = qByteArray2.data();
// 2.char*转QString
char* ch3 = "admin";
QString str3(ch3);
char* ch4 = "中国人";// 中文
QString str4 = QString::fromLocal8Bit(ch4);//error QString str4(ch4)
qDebug() << ch3 << ch4 << str3 << str4;//ch3 ch4 打印结果不正确
// 3.QString转char[]
QString str5 = "HH123456";
char ch5[2] = {0};
strcpy(ch5, str5.toLocal8Bit().data());
QString str6 = QStringLiteral("HH们们2");
char ch6[128] = {0};
QByteArray ba = str6.toLocal8Bit();
char* tempCh = ba.data();
strcpy(ch6, tempCh);
qDebug() << str5 << ch5[0] << ch5[1] << tempCh << ch6;//tempCh ch6 打印结果不正确
// 4.char[]转QString
char ch7[5] = "8005";// char ch6[4] = "8005"(数组界限溢出) 或者 char ch6[4] = {'8', '0', '0', '5'};
QString str7(ch7);
QMessageBox::information(NULL,"tip",str7);
char ch8[5] = "他们";
QString str8 = QString::fromLocal8Bit(ch8,5);
qDebug() << ch7 << " " << ch7[0] << ch7[1] << ch7[2] << ch7[3] << ch7[4] << str7;//8005 8005