MBCS字符转换为string类型的UTF8字符

来源:互联网 发布:淘宝上香港化妆品 编辑:程序博客网 时间:2024/04/28 18:54
string MbcsToUtf8(const char *str) {WCHAR*pwchar = 0;CHAR*pchar= 0;intlen= 0;intcodepage= AreFileApisANSI() ? CP_ACP :CP_OEMCP;stringret;len = MultiByteToWideChar(codepage, 0, str, -1, 0, 0);pwchar = new WCHAR[len];if (pwchar == 0) return ret;len = MultiByteToWideChar(codepage, 0, str, -1, pwchar, len);if (len != 0) {len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, 0, 0, 0, 0);pchar = new CHAR[len];if (pchar) {len = WideCharToMultiByte(CP_UTF8, 0, pwchar, -1, pchar, len, 0, 0);if (len) {ret = pchar;}delete []pchar;}}delete []pwchar;return ret;}

摘自http://blog.csdn.net/chinacodec/article/details/7246569 已去除内存泄露