#include "Windows.h" char* UnicodeToAnsi(const wchar_t* szStr) { ?? ?int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL); ?? ?if (nLen == 0) ?? ?{ ?? ? ? ?return NULL; ?? ?} ?? ?char* pResult = new char[nLen]; ?? ?WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL); ?? ?return pResult; } wchar_t* AnsiToUnicode(const char* szStr) { ?? ?int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0); ?? ?if (nLen == 0) ?? ?{ ?? ? ? ?return NULL; ?? ?} ?? ?wchar_t* pResult = new wchar_t[nLen]; ?? ?MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen); ?? ?return pResult; } qDebug() << "===================================="; //QString转string QString qstr1 = "demo"; std::string str1 = qstr1.toStdString(); qDebug() << QString::fromStdString(str1); //QString转wstring std::wstring wstr1 = qstr1.toStdWString(); qDebug() << QString::fromStdWString(wstr1); //QString转const char* std::string str2 = qstr1.toStdString(); const char* cstr1 = str2.c_str(); qDebug() << QString::fromStdString(cstr1); //QString转char* QByteArray bytearray1 = qstr1.toLocal8Bit(); char* cstr2 = bytearray1.data(); qDebug() << QString::fromLocal8Bit(cstr2); //QString转char[] char cstr3[128]; std::strcpy(cstr3, qstr1.toStdString().data()); qDebug() << QString::fromStdString(cstr3); //QString转uchar* QByteArray bytearray2 = qstr1.toLocal8Bit(); unsigned char* ustr1 = (unsigned char *)bytearray2.data(); qDebug() << QString::fromLocal8Bit((char*)ustr1); //QString转QByteArray QByteArray bytearray3 = qstr1.toLocal8Bit(); qDebug() << QString::fromLocal8Bit(bytearray3);
qDebug() << "===================================="; //string转QString std::string str3 = "demo"; QString qstr2 = QString::fromStdString(str3); qDebug() << qstr2; //string转const char* const char* cstr4 = str3.data(); qDebug() << QString::fromStdString(cstr4); //string转char* char cstr5[128]; strcpy(cstr5, str3.c_str()); qDebug() << QString::fromStdString(cstr5); //string转char* char* cstr6 = const_cast<char*>(str3.data()); qDebug() << QString::fromStdString(cstr6);
qDebug() << "===================================="; //const char*转QString const char* cstr7 = "demo"; QString qstr3 = QString(cstr7); qDebug() << qstr3; //char*转QString char* cstr8 = (char*)"demo"; QString qstr5 = QString(cstr8); qDebug() << qstr5; //char[]转QString char cstr9[128] = "demo"; QString qstr4 = QString(cstr9); qDebug() << qstr4;
qDebug() << "===================================="; //const char*转string const char* cstr10 = "demo"; std::string str4(cstr10); qDebug() << QString::fromStdString(str4); //char*转string char* cstr11 = (char*)"demo"; std::string str5(cstr11); qDebug() << QString::fromStdString(str5); //char[]转string char cstr12[128] = "demo"; std::string str6(cstr12); qDebug() << QString::fromStdString(str6);
qDebug() << "===================================="; //const char*转char* const char* cstr13 = "demo"; char* cstr14 = const_cast<char*>(cstr13); qDebug() << QString::fromStdString(cstr14); //char*转const char* char* cstr15 = (char*)"demo"; const char* cstr16 = cstr15; qDebug() << QString::fromStdString(cstr16); //char*转char[] char cstr17[128]; char* cstr18 = (char*)"demo"; strcpy(cstr17, cstr18); qDebug() << QString::fromStdString(cstr17); //char[]转char* char cstr19[128] = "demo"; char* cstr20 = cstr19; qDebug() << QString::fromStdString(cstr20);
qDebug() << "===================================="; //QByteArray转QString QByteArray bytearray4 = "demo"; QString qstr6 = QString(bytearray4); qDebug() << qstr6; //QByteArray转char* char* str21 = bytearray4.data(); qDebug() << QString::fromStdString(str21); //char*转QByteArray char* str22 = (char*)"demo"; QByteArray bytearray5; bytearray5 = QByteArray(str22); qDebug() << QString(bytearray5);
qDebug() << "===================================="; //QString转unsigned char QString qstr7 = "129"; bool ok1; unsigned char uchar1 = (unsigned char)qstr7.toUShort(&ok1, 16); qDebug() << uchar1; //十六进制转十进制 bool ok2; QString hexstr1 = "129"; int dec1 = hexstr1.toInt(&ok2, 16); qDebug() << QString::number(dec1); //十进制转十六进制 int dec2 = 297; QString hexstr2 = QString("%1").arg(dec2, 4, 16, QLatin1Char('0')); qDebug() << hexstr2;
qDebug() << "===================================="; //short int float double long转QString short test1 = 2; int test2 = 3; float test3 = 4.6; double test4 = 8.7; long test5 = 29999; qDebug() << QString::number(test1, 10) << QString::number(test2, 10) << QString::number(test3, 'f', 2) << QString::number(test4, 'f', 2) << QString::number(test5); //QString转short int float double long QString qstr8 = "22"; qDebug() << qstr8.toShort() << qstr8.toInt() << qstr8.toFloat() << qstr8.toDouble() << qstr8.toLong();
|