使用Qt开发嵌入式时,常引用一些C语言的驱动,那就涉及到QString和char *之间的互相转换,方法如下,直接上代码:
// char[]转QString unsigned char buf[3]={0x2,0x56,0xff}; QString str; for(int i =0;i < sizeof(buf)/sizeof(buf[0]);i++) { str += QString::number(buf[i]); } ui->textBrowser->append(str); //QString转char * QString str1 = "hello"; QByteArray ba = str1.toLatin1(); char *mm = ba.data(); qDebug()<<mm<<endl; //调试时,在console中输出 //char *转QString QString nn = QString(QLatin1String(mm)); qDebug()<<nn<<endl; //调试时,在console中输出 //QByteArray转char * QByteArray ba1("Hello world"); char *data = ba1.data(); while (*data) { qDebug() << "[" << *data << "]" << endl; ++data; }
时间: 2024-12-04 17:48:49