Last active
August 29, 2015 14:08
-
-
Save Temptationx/c11f037dfb2c8f6699f5 to your computer and use it in GitHub Desktop.
utf-8, gbk, utf-16 转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string GBKToUTF8(const char* strGBK) | |
{ | |
int len = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0); | |
wchar_t* wstr = new wchar_t[len+1]; | |
memset(wstr, 0, len+1); | |
MultiByteToWideChar(CP_ACP, 0, strGBK, -1, wstr, len); | |
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); | |
char* str = new char[len+1]; | |
memset(str, 0, len+1); | |
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); | |
string strTemp = str; | |
if(wstr) delete[] wstr; | |
if(str) delete[] str; | |
return strTemp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string UTF8ToGBK(const char* strUTF8) | |
{ | |
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0); | |
wchar_t* wszGBK = new wchar_t[len+1]; | |
memset(wszGBK, 0, len*2+2); | |
MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, wszGBK, len); | |
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); | |
char* szGBK = new char[len+1]; | |
memset(szGBK, 0, len+1); | |
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL); | |
string strTemp(szGBK); | |
if(wszGBK) delete[] wszGBK; | |
if(szGBK) delete[] szGBK; | |
return strTemp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wstring UTF8ToUTF16(const char *strUTF8) | |
{ | |
int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, NULL, 0); | |
wchar_t* wszGBK = new wchar_t[len + 1]; | |
memset(wszGBK, 0, len * 2 + 2); | |
MultiByteToWideChar(CP_UTF8, 0, strUTF8, -1, wszGBK, len); | |
wstring ret(wszGBK); | |
if (wszGBK) { | |
delete[] wszGBK; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment