1.char[] 转换为QString
法一:最保险的方法
char ch_data[512];
QString str_data;
............
str_data = QString::fromLocal8Bit(ch_data,512);
法二:c++ 强制转换
char a[]="123";
QString str=a;
其中:
a[0]='1' str[0]='1'
a[1]='2' str[1]='2'
a[2]='3' str[2]='3'
a[3]='\0'
str在赋值时,遇到'\0'停止。
2.string与int型转换
//int转string
int a = 123456;
string b;
b = std::tostring(a);
//string转int
string c = "123456";
int d;
d = atoi(c.c_str());//string转float 用 atof()
3.int与double之间转换
//int转double
int a = 1234;
double b;
b = a;//直接赋值就可以
//double转int
double c = 123.456;
int d;
d = c;//d的结果为123,即只取整数部份
d = c*1000;//乘1000将小数消掉即可,注意int位数要求,避免溢出
4.int与QString之间的转换
//int转QString
int a = 123456;
QString b;
b = QString::number(a,10,5);//QString::number(a,基底,精度)
//方法2,利用arg()
int a = 123456;
QString b = QString("%1").arg(a);
//QString转int
QString c = "123456";
int d;
d = c.toInt();
5.double与QString之间的转换
//double转QString
double a = 123.456;
QString b;
b = QString::number(a,10,5);//同int
//QString转double
QString c = "123.456";
double d;
d = c.toDouble();//类似int
6.string与QString之间的转换
//string转QString
string a = "123.456";
QString b;
b = QString::fromStdString(a);
//QString转string
QString c = "123,456";
string d;
d = c.toStdString();
7.QChar转换char
char = QChar.unicode()
8.int转16进制,并补0
QString key;
key = QString("%1").arg(k,4,16,QLatin1Char('0'));//k为int型或char型都可