您的位置:首页 > 其它

MFC开发IM-第二十一篇、Unicode转成Utf8

2018-03-21 16:25 183 查看
//UnicodeToUtf8 Unicode转成Utf8
char*  mainDlg::UnicodeToUtf8(const wchar_t* unicode)
{
    int len;
    len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
    char *szUtf8 = (char*)malloc(len + 1);
    memset(szUtf8, 0, len + 1);
    WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
    return szUtf8;
}

int length;
wchar_t* wCharUnicode = L"中国你好";
length = wcslen(wCharUnicode);                      // length = 4;

char* cCharUtf = UnicodeToUtf8(wCharUnicode);
length = strlen(cCharUtf);                          // length = 12;

// 将UTF格式的char*转为CString
CString strUtf(cCharUtf);
length = strUtf.GetLength();                        // length = 6;

//CString strUnicode = UTF82WCS(cCharUtf);
//length = strUnicode.GetLength();                    // length = 4;
//   wchar_t* wCharUnicode = L"中国";
      // char* cCharUtf = UnicodeToUtf8(wCharUnicode);
const char* c_s ="是";
const char* cpc;

char* pc="abcde";

cpc=cCharUtf;

    send(main_socket, cpc,length, 0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: