C++ ansi转utf8、utf8转ansi相互转换
C++ ansi转utf8、utf8转ansi相互转换
void Convert(const char* strIn,char* strOut, int sourceCodepage, int targetCodepage)
{
int len = lstrlenA(strIn);
int unicodeLen= MultiByteToWideChar(sourceCodepage, 0, strIn, -1, NULL, 0);
wchar_t pUnicode = {0};
MultiByteToWideChar(sourceCodepage, 0, strIn, - 1, (LPWSTR)pUnicode, unicodeLen);
BYTE pTargetData = {0};
int targetLen = WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1, (char*)pTargetData,0, NULL, NULL);
WideCharToMultiByte(targetCodepage, 0, (LPWSTR)pUnicode, -1,(char*)pTargetData, targetLen, NULL, NULL);
lstrcpyA(strOut,(char*)pTargetData);
}
void UTF82Ansi(PCHAR Src, PCHAR Dst)
{
Convert(Src,Dst,CP_UTF8,CP_ACP);
}
void Ansi2UTF8(PCHAR Src, PCHAR Dst)
{
Convert(Src,Dst,CP_ACP, CP_UTF8);
}
void TestFunc()
{
// ansi转utf8
char* ansi_str = "ansi_str ansi字符串";
char utf_buf = {0};
Ansi2UTF8(ansi_str, utf_buf);
// utf8转ansi
char ansi_buf= {0};
UTF82Ansi(utf_buf, ansi_buf);
}
页:
[1]