C++ 字符串/内存数据编码转换(ANSI、UNICODE、UTF8)

"这篇博客介绍了用于ANSI与UNICODE以及UTF8之间转换的C++函数,适用于带有结束符''的字符串。函数通过MultiByteToWideChar和WideCharToMultiByte API实现,对于内存中可能含有结束符的数据,提供了带长度参数的版本确保转换的准确性。这些函数对于处理不同编码格式的字符串非常实用。"
摘要由CSDN通过智能技术生成

1、对于字符串来说,并且最后带有结束符:'\0'的字符串,使用下面的两个函数即可:

std::string ANSItoUTF8(const char * ansi)
{
	int len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_ACP, 0, ansi, -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;
	std::string ret = str;
	if(str) delete[] str;
	return ret;
}
std::string UTF8toANSI(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;
	std::string ret = str;
	if(str) delete[] str;
	return ret;
}

ANSI和UNICODE的转换以及UNICODE与UTF8之间的转换,就是上面两个函数的简化版本,我们应该可以自行写出。

2、对于内存中的数据来说,比如我们将一张图片读入到内存,因为在数据的中间某个位置可能存在结束符:'\0',所以上面两个函数就失效了,但是我们可以对上面两个函数稍加变换即可:

std::string ANSItoUTF8(const char * ansi, int iSize, int& oSize)
{
	int lenUNICODE = MultiByteToWideChar(CP_ACP, 0, ansi, iSize, NULL, 0);
	wchar_t* wstr = new wchar_t[lenUNICODE + 1];
	memset(wstr, 0, lenUNICODE + 1);
	lenUNICODE = MultiByteToWideChar(CP_ACP, 0, ansi, iSize, wstr, lenUNICODE);

	int lenUTF8 = WideCharToMultiByte(CP_UTF8, 0, wstr, lenUNICODE, NULL, 0, NULL, NULL);
	char* str = new char[lenUTF8 + 1];
	memset(str, 0, lenUTF8 + 1);
	lenUTF8 = WideCharToMultiByte(CP_UTF8, 0, wstr, lenUNICODE, str, lenUTF8, NULL, NULL);

	oSize = lenUTF8;

	std::string ret(str, lenUTF8);

	if (wstr) delete[] wstr;
	if (str) delete[] str;
	return ret;
}
std::string UTF8toANSI(const char* utf8, int iSize, int& oSize)
{
	int lenUNICODE = MultiByteToWideChar(CP_UTF8, 0, utf8, iSize, NULL, 0);
	wchar_t* wstr = new wchar_t[lenUNICODE + 1];
	memset(wstr, 0, lenUNICODE + 1);
	lenUNICODE = MultiByteToWideChar(CP_UTF8, 0, utf8, iSize, wstr, lenUNICODE);

	int lenANSI = WideCharToMultiByte(CP_ACP, 0, wstr, lenUNICODE, NULL, 0, NULL, NULL);
	char* str = new char[lenANSI + 1];
	memset(str, 0, lenANSI + 1);
	lenANSI = WideCharToMultiByte(CP_ACP, 0, wstr, lenUNICODE, str, lenANSI, NULL, NULL);

	oSize = lenANSI;
	std::string ret(str, lenANSI);

	if (wstr) delete[] wstr;
	if (str) delete[] str;
	return ret;
}

上面这两个函数对于带结束符的字符串也是通用的。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小米的修行之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值