MBCS 문자를string 형식의 UTF8 문자로 변환

876 단어 Stringdelete
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메모리 유출 제거

좋은 웹페이지 즐겨찾기