怎么让C++中字符串编码为utf8,而不是ansi
1个回答
展开全部
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[1024] = {0}; MultiByteToWideChar(sourceCodepage, 0, strIn, - 1, (LPWSTR)pUnicode, unicodeLen); BYTE pTargetData[2048] = {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[MAX_PATH] = {0}; Ansi2UTF8(ansi_str, utf_buf); // utf8转ansi char ansi_buf[MAX_PATH] = {0}; UTF82Ansi(utf_buf, ansi_buf);}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询