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

string MbcsToUtf8(const char *str) {
	WCHAR	*pwchar 	= 0;
	CHAR	*pchar	= 0;
	int	len	= 0;
	int	codepage	= AreFileApisANSI() ? CP_ACP :CP_OEMCP;
	string	ret;

	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 已去除内存泄露

你可能感兴趣的:(String,delete)