C++编码转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Houheshuai/article/details/73321951

在使用VS2013时从文本读取的 UTF-8 的编码格式的文字,要转换成 GBK 的,这里讲部分编码转换的方法贴下
string GBKToUTF8(const std::string& strGBK)
{
	string strOutUTF8 = "";
	WCHAR * str1;
	int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
	str1 = new WCHAR[n];
	MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
	n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
	char * str2 = new char[n];
	WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
	strOutUTF8 = str2;
	delete[]str1;
	str1 = NULL;
	delete[]str2;
	str2 = NULL;
	return strOutUTF8;
}

char * UTF8ToUnicode( char* str)
{
	int textlen;
	char * result;
	textlen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	result = (char*)malloc((textlen + 1)*sizeof(wchar_t));
	memset(result, 0, (textlen + 1)*sizeof(wchar_t));
	MultiByteToWideChar(CP_UTF8, 0, str, -1, (LPWSTR)result, textlen);
	return result;
}

char * UnicodeToUTF8(const wchar_t* str)
{
	char* result;
	int textlen;
	textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
	result = (char *)malloc((textlen + 1)*sizeof(char));
	memset(result, 0, sizeof(char) * (textlen + 1));
	WideCharToMultiByte(CP_UTF8, 0, str, -1, result, textlen, NULL, NULL);
	return result;
}


还有一种使用icov转换的

int convert(const char *from, const char *to, char* save, int savelen, char *src, int srclen)
{
	iconv_t cd;
	char   *inbuf = src;
	char *outbuf = save;
	size_t outbufsize = savelen;
	int status = 0;
	size_t  savesize = 0;
	size_t inbufsize = srclen;
	const char* inptr = inbuf;
	size_t      insize = inbufsize;
	char* outptr = outbuf;
	size_t outsize = outbufsize;
	cd = iconv_open(to, from);
	iconv(cd, NULL, NULL, NULL, NULL);
	if (inbufsize == 0) {
		status = -1;
		goto done;
	}
	while (insize > 0) {
		size_t res = iconv(cd, (const char**)&inptr, &insize, &outptr, &outsize);
		if (outptr != outbuf) {
			int saved_errno = errno;
			int outsize = outptr - outbuf;
			strncpy(save + savesize, outbuf, outsize);
			errno = saved_errno;
		}
		if (res == (size_t)(-1)) {
			if (errno == EILSEQ) {
				int one = 1;
				//iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &one);
				status = -3;
			}
			else if (errno == EINVAL) {
				if (inbufsize == 0) {
					status = -4;
					goto done;
				}
				else {
					break;
				}
			}
			else if (errno == E2BIG) {
				status = -5;
				goto done;
			}
			else {
				status = -6;
				goto done;
			}
		}
	}
	status = strlen(save);
done:
	iconv_close(cd);
	return status;
}


猜你喜欢

转载自blog.csdn.net/Houheshuai/article/details/73321951