具体代码是:
#include <stdio.h> #include <stdlib.h> #include <wchar.h> #include <locale.h> int char2wchar(char* pSrc, wchar_t* pDest) { if (pSrc == NULL || pDest == NULL) { return 0; } setlocale(LC_CTYPE, "zh_CN.utf8"); int w_size = mbstowcs(NULL, pSrc, 0) + 1; //w_size=0说明出错了。可能有非法字符,也可能是locale设置不对。 if (w_size == 0) { return -1; } int ret = mbstowcs(pDest, pSrc, strlen(pSrc)+1); if (ret <= 0) { return -1; } return w_size; } int main() { wchar_t wstr[32] = {0}; char* source = "泰山"; int count = char2wchar(source, &(wstr[0])); }