【Qt】QString,std::string,char中文字符互相转换

关于QString显示中文字符不乱码的问题,参见:http://blog.csdn.net/shihoongbo/article/details/47677159


#include 
#include 
#include 
#include 
#include 

std::string QString2stdString(QString qstr)
{
	std::string stdStr = qstr.toLocal8Bit().data();
	return stdStr;
}

QString stdString2QString(std::string stdStr)
{
	QString qstr = QString::fromLocal8Bit(stdStr.c_str());
	return qstr;
}

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	QString qstr = u8"中文字符";
	
	std::cout << QString2stdString(qstr) << std::endl;
	std::cout << QString2stdString(qstr).c_str() << std::endl;

	std::cout << std::endl;

	char *cstr = { "中文字符" };
	std::cout << cstr << std::endl;
	std::string stdStr(cstr);
	std::cout << stdStr << std::endl;
	qDebug() << stdString2QString(stdStr) << endl;

	return a.exec();
}


你可能感兴趣的:(Qt)