1.从QString到std::string

1
2
std::string oldString = "你好,hello world";
QString newString = QString::fromLocal8Bit(oldString.c_str())

2.从std::string到QString

1
2
QString oldString = "你好,hello world";
std::string newString = std::string((const char *)oldString.toLocal8Bit().constData());

3.从std::string到char *

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char *wchar2char(const wchar_t* source)
{
char * data;
int len= WideCharToMultiByte( CP_ACP ,0,source ,wcslen( source ), nullptr,0, nullptr ,nullptr);
data= new char[len+1];
WideCharToMultiByte( CP_ACP ,0,source ,wcslen( source ),data,len, nullptr ,nullptr);
data[len]= '\0';
return data;
}

const char *stringToChar(const std::string& str)
{
std::wstring wstr = L"";
int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), nullptr, 0);
wchar_t* wchar = new wchar_t[len + 1];
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size(), wchar, len);
wchar[len] = '\0';
wstr.append(wchar);
const char *data = wchar2char(wchar);
delete[] wchar;
return data;
}

© 2025 hywing 使用 Stellar 创建
总访问 113701 次 | 本页访问 326