Dam*_*ian 2 c++ ascii utf-8 character-encoding
我无法在任何地方找到这个问题的答案.
如何在C++中将字符串从UTF-8转换为ANSI(扩展ASCII)?
仅限 Windows:
string UTF8ToANSI(string s)
{
BSTR bstrWide;
char* pszAnsi;
int nLength;
const char *pszCode = s.c_str();
nLength = MultiByteToWideChar(CP_UTF8, 0, pszCode, strlen(pszCode) + 1, NULL, NULL);
bstrWide = SysAllocStringLen(NULL, nLength);
MultiByteToWideChar(CP_UTF8, 0, pszCode, strlen(pszCode) + 1, bstrWide, nLength);
nLength = WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, NULL, 0, NULL, NULL);
pszAnsi = new char[nLength];
WideCharToMultiByte(CP_ACP, 0, bstrWide, -1, pszAnsi, nLength, NULL, NULL);
SysFreeString(bstrWide);
string r(pszAnsi);
delete[] pszAnsi;
return r;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10149 次 |
| 最近记录: |