UTF8和ANSI互转


std::string Utf8ToAnsi(const char* szUtf8)
{
	bool bNeedFreeW = false;
	int WLength = MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, NULL, NULL);
	LPWSTR pszW = NULL;
	__try
	{
		pszW = (LPWSTR)_alloca((WLength + 1) * sizeof(WCHAR));
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		pszW = (LPWSTR)malloc((WLength + 1) * sizeof(WCHAR));
		bNeedFreeW = true;
	}
	MultiByteToWideChar(CP_UTF8, 0, szUtf8, -1, pszW, WLength);
	pszW[WLength] = '\0';

	bool bNeedFreeA = false;
	int ALength = WideCharToMultiByte(CP_ACP, 0, pszW, -1, NULL, 0, NULL, NULL);
	LPSTR pszA = NULL;
	__try
	{
		pszA = (LPSTR)_alloca(ALength + 1);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		pszA = (LPSTR)malloc(ALength + 1);
		bNeedFreeA = true;
	}
	WideCharToMultiByte(CP_ACP, 0, pszW, -1, pszA, ALength, NULL, NULL);
	pszA[ALength] = '\0';

	std::string retStr = pszA;

	if (bNeedFreeW)
	{
		free(pszW);
	}

	if (bNeedFreeA)
	{
		free(pszA);
	}

	return retStr;
}

std::string AnsiToUtf8(const char* szAnsi)
{
	bool bNeedFreeW = false;
	int WLength = MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, NULL, 0);
	LPWSTR pszW = NULL;
	__try
	{
		pszW = (LPWSTR)_alloca((WLength + 1) * sizeof(WCHAR));
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		pszW = (LPWSTR)malloc((WLength + 1) * sizeof(WCHAR));
		bNeedFreeW = true;
	}
	MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, pszW, WLength);

	bool bNeedFreeA = false;
	int ALength = WideCharToMultiByte(CP_UTF8, 0, pszW, -1, NULL, 0, NULL, NULL);
	LPSTR pszA = NULL;
	__try
	{
		pszA = (LPSTR)_alloca(ALength + 1);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		pszA = (LPSTR)malloc(ALength + 1);
		bNeedFreeA = true;
	}
	WideCharToMultiByte(CP_UTF8, 0, pszW, -1, pszA, ALength, NULL, NULL);
	pszA[ALength] = '\0';

	std::string retStr(pszA);

	if (bNeedFreeW)
	{
		free(pszW);
	}
	
	if (bNeedFreeA)
	{
		free(pszA);
	}

	return retStr;
}

如果编译报错:

则将 项目 → 属性 → C/C++ → 代码生成 → 启用C++异常,改成“否”

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UTF-8与ANSI是不同的字符编码方式,UTF-8支持全球范围内的字符,而ANSI只支持ASCII码字符集。因此,将UTF-8编码换为ANSI编码需要进行字符集的换。 以下是一些实现UTF-8ANSI的示例方法: 方法一:使用Windows API函数进行编码换 在Windows平台上,可以使用Windows API函数WideCharToMultiByte和MultiByteToWideChar进行编码换。此方法需要包含Windows.h头文件。 ```c #include <windows.h> #include <stdio.h> int main() { // UTF-8字符串 char utf8_str[] = "你好,世界!"; // 将UTF-8字符串换为Unicode字符串 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, NULL, 0); wchar_t* wide_str = (wchar_t*)malloc(wide_len * sizeof(wchar_t)); MultiByteToWideChar(CP_UTF8, 0, utf8_str, -1, wide_str, wide_len); // 将Unicode字符串换为ANSI字符串 int ansi_len = WideCharToMultiByte(CP_ACP, 0, wide_str, -1, NULL, 0, NULL, NULL); char* ansi_str = (char*)malloc(ansi_len * sizeof(char)); WideCharToMultiByte(CP_ACP, 0, wide_str, -1, ansi_str, ansi_len, NULL, NULL); printf("%s\n", ansi_str); free(wide_str); free(ansi_str); return 0; } ``` 方法二:使用iconv库进行编码换 iconv是一个开源的字符集换库,可以在多个操作系统平台上使用。该库可以使用iconv_open、iconv、iconv_close等函数进行字符集换。此方法需要包含iconv.h头文件,并需要在编译时链接libiconv库。 ```c #include <iconv.h> #include <stdio.h> #include <stdlib.h> int main() { // UTF-8字符串 char utf8_str[] = "你好,世界!"; // 将UTF-8字符串换为ANSI字符串 char* ansi_str = (char*)malloc(sizeof(char) * 256); char* inbuf = utf8_str; char* outbuf = ansi_str; size_t inlen = strlen(utf8_str); size_t outlen = 256; iconv_t cd = iconv_open("CP936", "UTF-8"); iconv(cd, &inbuf, &inlen, &outbuf, &outlen); iconv_close(cd); printf("%s\n", ansi_str); free(ansi_str); return 0; } ``` 以上两种方法均可以将UTF-8编码换为ANSI编码。其中,Windows API函数方法只适用于Windows平台,而iconv库方法可以在多个平台上使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值