VC中Unicode字符集转UTF-8字符集

来源:互联网 发布:随机过程教材 知乎 编辑:程序博客网 时间:2024/06/13 04:21

1. CString类型

char* CStringToChar(CString csData){ DWORD dwNum = WideCharToMultiByte(CP_UTF8,NULL,csData,-1,NULL,NULL,0,NULL);char *cResult = (char *)malloc(dwNum+1);  memset(cResult, 0, dwNum+1);  WideCharToMultiByte(CP_UTF8,NULL,csData,-1,cResult,dwNum,0,NULL);return cResult;}

2. char*类型

char* UnicodeToUTF8(const char *str){wchar_t*  wideStr = NULL; char*   utf8Str = NULL; int   charLen = -1;charLen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); wideStr = (wchar_t*) malloc(sizeof(wchar_t)*charLen); MultiByteToWideChar(CP_ACP, 0, str, -1, wideStr, charLen);charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL);utf8Str = (char*) malloc(charLen);WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, utf8Str, charLen, NULL, NULL);free(wideStr); return utf8Str;}


0 0