本文主要是介绍utf-8和GB2312互转,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自CSDN,出处未知
UTF-8转GB2312:
char* convertUTF8ToGB2312(const char* utf8)
{int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);char* str = new char[len+1];memset(str, 0, len+1);WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);if(wstr) delete[] wstr;return str;
}
GB2312转UTF-8:
char* convertGB2312ToUTF8(const char* gb2312)
{int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);wchar_t* wstr = new wchar_t[len+1];memset(wstr, 0, len+1);MultiByteToWideChar(CP_ACP, 0, gb2312, -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);if(wstr) delete[] wstr;return str;
}
这篇关于utf-8和GB2312互转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!