今天总结一下qt和std c++各种数据类型的转换
一、QString和char*
1、QString转化为QByteArray类型,然后由QByteArray再转化为char* 例:
QString dome0;
char dome1[20];
QByteArray dome2 = dome0.toLocal8Bit();
strcpy(dome1, dome2.data());
2、QString转化为string类型,然后由string转化char* 例
QString dome0;
char dome1[20];
std::string dome2 = dome0.toStdString();
dome1 = dome2.c_str();
3、char*转化为QString
QString dome1;
char demo2[20];
dome1 = QString("%1").arg(demo2);
1、QString转换string
string dome1;
QString dome2;
dome1 = dome2.toStdstring(dome2);
2、string转化为QString
string dome1;
QString dome2;
dome2 = QString::fromestdstring(dome1);
三、string和char*
1、string转化char*
string dome1;
char dome2[20];
dome = dome1.c_str();
2、char*转化为string
char dome1[20] = "hello world!";
string dome2(dome1);
四、string和int
1、int转化为string
int aa = 30;
stringstream ss;
ss<<aa;
string s1 = ss.str();
cout<<s1<<endl; // 30
2、string转化为int
int dome;
sscanf("123","%D",&dome);