int AsciToUtf8(char* pSrc, unsigned int nSrcLen, char* pBuffer, unsigned int nBufferLen) { assert(pSrc != NULL); int nRet(0); int nUtf16Len = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLen, NULL, 0); if(nUtf16Len > 0) { wchar_t* pW = new wchar_t[nUtf16Len]; if(pW != NULL) { nUtf16Len = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLen, pW, nUtf16Len); if(pBuffer == NULL || nBufferLen <= 0) { nRet = WideCharToMultiByte(CP_UTF8, 0, pW, nUtf16Len, NULL, 0, NULL, NULL); } else { nRet = WideCharToMultiByte(CP_UTF8, 0, pW, nUtf16Len, pBuffer, nBufferLen, NULL, NULL); } delete []pW; pW = NULL; } } return nRet; } int main() { char* p = "abc,你好"; int nLen = lstrlen(p) + 1; //include end flag '\0' int nBufferLen = AsciToUtf8(p, nLen, NULL, 0); //get need buffer length char* pBuffer = new char[nBufferLen]; AsciToUtf8(p, nLen, pBuffer, nBufferLen); delete []pBuffer; system("pause"); return 0; }