龙空技术网

Unicode 与 UTF-8 编码互转

科技是第1生产力 58

前言:

如今各位老铁们对“unicode转换utf8php”大体比较看重,大家都需要分析一些“unicode转换utf8php”的相关知识。那么小编同时在网络上搜集了一些有关“unicode转换utf8php””的相关文章,希望姐妹们能喜欢,看官们一起来学习一下吧!

Unicode 转 UTF-8

char* 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 main(int argc, char *argv[]){    wchar_t* wCharUnicode = L"中国";    char* cCharUtf = UnicodeToUtf8(wCharUnicode);    return 0;}
UTF-8 转 Unicode
CString UTF82WCS(const char* szU8){    //预转换,得到所需空间的大小;    int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), NULL, 0);    //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间    wchar_t* wszString = new wchar_t[wcsLen + 1];    //转换    ::MultiByteToWideChar(CP_UTF8, NULL, szU8, strlen(szU8), wszString, wcsLen);    //最后加上'\0'    wszString[wcsLen] = '\0';    CString unicodeString(wszString);    delete[] wszString;    wszString = NULL;    return unicodeString;}int main(int argc, char *argv[]){    wchar_t* wCharUnicode = L"中国";    char* cCharUtf = UnicodeToUtf8(wCharUnicode);    CString strUnicode = UTF82WCS(cCharUtf);    return 0;}
测试效果:
int length;wchar_t* wCharUnicode = L"中国你好";length = wcslen(wCharUnicode);                      // length = 4;char* cCharUtf = UnicodeToUtf8(wCharUnicode);length = strlen(cCharUtf);                          // length = 12;// 将UTF格式的char*转为CStringCString strUtf(cCharUtf);length = strUtf.GetLength();                        // length = 6;CString strUnicode = UTF82WCS(cCharUtf);length = strUnicode.GetLength();                    // length = 4;
int length;wchar_t* wCharUnicode = L"中国,你好abc";length = wcslen(wCharUnicode);                      // length = 8;char* cCharUtf = UnicodeToUtf8(wCharUnicode);length = strlen(cCharUtf);                          // length = 16;// 将UTF格式的char*转为CStringCString strUtf(cCharUtf);length = strUtf.GetLength();                        // length = 10;CString strUnicode = UTF82WCS(cCharUtf);length = strUnicode.GetLength();                    // length = 8;
int length;wchar_t* wCharUnicode = L"中国,你好abc";length = wcslen(wCharUnicode);                      // length = 8;char* cCharUtf = UnicodeToUtf8(wCharUnicode);length = strlen(cCharUtf);                          // length = 18;// 将UTF格式的char*转为CStringCString strUtf(cCharUtf);length = strUtf.GetLength();                        // length = 10;CString strUnicode = UTF82WCS(cCharUtf);length = strUnicode.GetLength();                    // length = 8;

标签: #unicode转换utf8php