1、QString转QByteArray
QByteArray byte;
QString string;
byte = string.toLatin1();
2、QByteArray转QString
QByteArray byte;
QString string;
string = QString(byte);
3、QString转String
QString string;
std::string str;
str = string.toStdString();
4、String转QString
QString string;
std::string str;
string = QString::fromStdString(str);
5、QByteArray转char *
char *ch; //不能定义成ch[n],数组和指针不一样;
QByteArray byte;
ch = byte.data();
6、char *转QByteArray
char *ch;
QByteArray byte;
byte = QByteArray(ch);
7、QString转char *
QString string;
char *ch;
ch = string.toLatin1.data();
8、char *转QString
char *ch;
QString string= QString(QLatin1String(ch));
9、QHostAddress转QString
QHostAddress address;
QString string = address.toString();
10、QString转QHostAddress
QString string;
QHostAddress address(string);
11、QString转unsigned char
QString string;
bool ok;
unsigned char uch = string.toInt(&ok, 16);
12、unsigned char转QString
unsigned char uch;
QString string = QString::number(uch,16);
13、int转QString
int m;
QString string;
string=QString::number(m);
14、QString转int
QString string;
int m;
m=string.toInt();
11和12主要用在串口通信中,把收到的数据转成QString后用于显示。
其实,number这个函数不仅仅能够转换成int型,还能转换成double,long等等,还可以16进制转换,非常强大。