windows chcp linuxlocale可以查看系统的编码方式。
QString的内部编码方式是unicode,当char* 或std::string 转换成-QString时,默认char* 或std::string的编码方式为UTF-8.
dialog->setLabelText(QFileDialog::Accept, 保存(&S));
QString text = "A\tA";
//4代表宽度,10表示10进制,空位补零
QString str=QString("\"date -s %1-%2-%3 %4:%5:%6\"").arg(ui->spinBox_systemtime1->value(),4, 10, QLatin1Char('0'))
.arg(ui->spinBox_systemtime2->value(),2, 10, QLatin1Char('0'))
.arg(ui->spinBox_systemtime3->value(),2, 10, QLatin1Char('0'))
.arg(ui->spinBox_systemtime4->value(),2, 10, QLatin1Char('0'))
.arg(ui->spinBox_systemtime5->value(),2, 10, QLatin1Char('0'))
.arg(ui->spinBox_systemtime6->value(),2, 10, QLatin1Char('0'));
QString(%1).arg(FullVersion[1], 2, 10, QChar('0')) //2代表宽度,10表示10进制,空位补 QChar('0')
//实现了从本地字符集GBK到Unicode的转换,
QString recordData = QString::fromLocal8Bit((const char*)&stFrame->data[5],stFrame->head.len-5);
QString QString::arg(uint a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const
fieldWidth :格式化后的字符串宽度;
base :指定将整数a转换为字符串时使用的基数(默认是十进制),还可以是二进制2 ,十六进制 16;
fillChar :待填充字符
//默认数值是3,填充长度为4,显示为10进制,长度不够就补0,最后字符串为“0003”
int num = 3;
QString str = QString("%1").arg(num, 4, 10, QChar('0')); //补齐长度
qDebug() << str; // str == "0003"
//可通过QString::number来决定保留小数点的位数
QString str = QString::number(32, 'f', 2);
qDebug() << str; // str == "32.00"
//将10进制的63变成16进制的3f
QString str;
str = QString("Decimal 63 is %1 in hexadecimal").arg(63, 0, 16);
qDebug() << str; // str == "Decimal 63 is 3f in hexadecimal"
//还可以根据不同国家的千位分隔符显示数值字符串
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
QString str = QString("normal:%1 long split: %L2 hexadecimal: %L3")
.arg(12345)
.arg(12345)
.arg(12345, 0, 16);
qDebug() << str; // str == "normal:12345 long split: 12,345 hexadecimal: 3039"
//arg %1 %2…%99位置替换为字符
//首先,arg(my)替换%1。然后arg(he)替换%2。最后,arg(word)替换%3。
QString my = "my";
QString he = " Hello ";
QString word = "Word";
QString str = QString("Look this is %1 And %2 : %3").arg(my).arg(he).arg(word);
qDebug() << str; // 输出 str = "Look this is my And Hello : Word"
int seconds = m_calc.elapsed() / 1000;
int hour = int(seconds / 3600);
int min = int((seconds - hour * 3600) / 60);
int sec = seconds % 60;
//QString格式化arg前面自动补0
QString str = QString("%1:%2:%3").arg(hour, 2, 10, QLatin1Char('0')).arg(min, 2, 10, QLatin1Char('0')).arg(sec, 2, 10, QLatin1Char('0'));
QStringList list;
list << tr("%1").arg(hour) << tr("%1").arg(min) << tr("%1").arg(sec);
int val = 16;
//0f
QString str = QString("%1").arg(val & 0xFF, 2, 16, QLatin1Char('0'));
//0016
QString str = QString("%1").arg(val, 4, 10, QLatin1Char('0'));
//4代表宽度,10表示10进制,空位补零
QString str;
char* ch;
QByteArray ba = str.toLatin1(); // mustd
ch=ba.data();
方法2.QString 转换为 char *
QString s2 = "Hello World 2!";
QByteArray ba2;
ba2.append(s2); //也可以 ba2 = s2.toLatin1();
const char *c2 = ba2.data();
qDebug() << s2 << c2;
QString filename;
std::string str = filename.toStdString();
const char* ch = str.c_str();
3.char * 转换为 QString
QString(const QLatin1String &str);
QLatin1String 的构造函数:QLatin1String(const char *str);
str = QString(QLatin1String(ch));
const char *c1 = "Hello World 1!";
QString s1(c1);
qDebug() << c1 << s1;
char ch_data[512];
QString str_data;
str_data = QString::fromLocal8Bit(ch_data,512);
中文都ok
uint8_t wifiName[MAX_NAME_LENGTH];
uint8_t wifiPassWord[MAX_NAME_LENGTH];
QString::fromUtf8((char*)wifiName)
typedef struct
{
uchar admission[20]; //住院号
//DevType type, //设备类型
uchar type; //设备类型 devstat, syb, zsb, tci, hub,
uchar mode; //设备型号 "TMS-1 =1,TMS-3 =3,TMS-5 =5"
char sn[30]; //设备序号
}Com_DevInfo;
qDebug() << "m_devInfo" << QString(m_devInfo.sn) << endl;
qDebug() << "m_devInfo" << QString::fromLocal8Bit(m_devInfo.sn) << endl;
qDebug() << "m_devInfo" << QString((char*)m_devInfo.admission) << endl;
qDebug() << "m_devInfo" << m_devInfo.type << endl;
qDebug() << "m_devInfo" << m_devInfo.mode << endl;
输出结果:
m_devInfo "2001"
m_devInfo "2001"
m_devInfo "abcdefg"
quint8 data[100]={'1','2','3','1','2','3','1','2','3','1','2','3','1','2','3'};
QByteArray qa;
for (int i=0;i<100;i++) {
qa.append(data[i]);
}
QString recordData(qa);
qDebug() << recordData << endl; //123123123123123
memcpy(net.wifiName,configJson->getKeyStringVal(wifiName).toLatin1().data(),configJson->getKeyStringVal(wifiName).size());
memcpy(net.wifiPassWord,configJson->getKeyStringVal(wifiPassWord).toLatin1().data(),configJson->getKeyStringVal(wifiPassWord).size());
QString swifi((char*)net.wifiName); QString swifip((char*)net.wifiPassWord);
qDebug() << net.wifiName << swifi << endl;
qDebug() << net.wifiPassWord << swifip << endl; //出现乱码 net.wifiPassWord要清0 NetInfor net; memset(&net,0,sizeof(NetInfor)); 然后在赋值
数字转字节数组。支持基数 2 到 36。
qDebug()<< QByteArray::number(0xaceb,2);
qDebug()<< QByteArray::number(0xaceb,10);
qDebug()<< QByteArray::number(0xaceb,16);
十六进制编码使用数字 0-9 和字母 a-f(小写)。如果分隔符不是 '\0',则分隔符插入到十六进制字节之间。
QByteArray macAddress = QByteArray::fromHex("123456abcdef");
qDebug()<< macAddress.toHex(); //returns "123456abcdef"
qDebug()<< macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
qDebug()<< macAddress.toHex(0); // returns "123456abcdef"
55
56
66
struct msgBody
{
quint32 msgLen; //消息长度
quint8 data[NET_SIZE]; //消息内容
};
qDebug() << msgFramePackage << QByteArray((char*)frame->body.data,frame->body.msgLen) << endl;