C++_CSting转换UTF-8,UTF-16相关

本文介绍了一种实现中文字符串与UTF-8编码互相转换的方法。通过具体的C/C++函数示例,展示了如何从多字节编码转换到UTF-8,并提供了标准字符串类与UTF-8之间的转换实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

中文转UTF-8

// 转化函数
void MBSToUTF8(char * utf8, int size, const char* mbs)
{
	if (!utf8 || !mbs)
		return;

	UINT nACP = GetACP();

	int dwNum = MultiByteToWideChar(nACP, 0, mbs, -1, NULL, 0);   
	if (dwNum <= 0)
		return;

	wchar_t* pwText = NULL;
	pwText = new wchar_t[dwNum + 1];
	if (!pwText)
	{
		delete[] pwText;
		return;
	}
	memset(pwText, 0, dwNum * 2 + 2);
	if (MultiByteToWideChar(nACP, 0, mbs, -1, pwText, dwNum) == 0)   // CP_ACP  
	{
		delete[] pwText;
		return;
	}

	int dwCount = WideCharToMultiByte(CP_UTF8, 0, pwText, -1, NULL, 0, NULL, NULL);
	if (dwCount <= 0)
	{
		delete[] pwText;
		return;
	}

	if (size < dwCount + 1)
	{
		delete[] pwText;
		return;
	}
	memset(utf8, 0, dwCount + 1);
	if (WideCharToMultiByte(CP_UTF8, 0, pwText, -1, utf8, dwCount, NULL, NULL) == 0)
	{
		delete[] pwText;
		return;
	}

	delete[] pwText;
}

///
// 调用
{
	CString sType = L"中文字符串";
	char utf1[64] = { 0 };  // 根据中文字符串长度,调整utf1长度
	MBSToUTF8(utf1, sizeof(utf1), CW2A(sType));
}

CString与UTF-8互相转化

std::string ConvertCStringToUTF8(CString strValue)
{
	std::wstring wbuffer;
#ifdef _UNICODE
	wbuffer.assign(strValue.GetString(), strValue.GetLength());
#else
	/*
	* 转换ANSI到UNICODE
	* 获取转换后长度
	*/
	int length = ::MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, (LPCTSTR)strValue, -1, NULL, 0);
	wbuffer.resize(length);
	/* 转换 */
	MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strValue, -1, (LPWSTR)(wbuffer.data()), wbuffer.length());
#endif

	/* 获取转换后长度 */
	int length = WideCharToMultiByte(CP_UTF8, 0, wbuffer.data(), wbuffer.size(), NULL, 0, NULL, NULL);
	/* 获取转换后内容 */
	std::string buffer;
	buffer.resize(length);

	WideCharToMultiByte(CP_UTF8, 0, strValue, -1, (LPSTR)(buffer.data()), length, NULL, NULL);
	return(buffer);
}

CString ConvertUTF8ToCString(std::string utf8str)
{
	/* 预转换,得到所需空间的大小 */
	int nLen = ::MultiByteToWideChar(CP_UTF8, NULL,
		utf8str.data(), utf8str.size(), NULL, 0);
	/* 转换为Unicode */
	std::wstring wbuffer;
	wbuffer.resize(nLen);
	::MultiByteToWideChar(CP_UTF8, NULL, utf8str.data(), utf8str.size(),
		(LPWSTR)(wbuffer.data()), wbuffer.length());

#ifdef UNICODE
	return(CString(wbuffer.data(), wbuffer.length()));
#else
	/*
	* 转换为ANSI
	* 得到转换后长度
	*/
	nLen = WideCharToMultiByte(CP_ACP, 0,
		wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL);

	std::string ansistr;
	ansistr.resize(nLen);

	/* 把unicode转成ansi */
	WideCharToMultiByte(CP_ACP, 0, (LPWSTR)(wbuffer.data()), wbuffer.length(),
		(LPSTR)(ansistr.data()), ansistr.size(), NULL, NULL);
	return(CString(ansistr.data(), ansistr.length()));
#endif
}

UTF-8转UTF-16:

wstring UTF8ToUTF16(const char* szIn)
{
	wstring strResult;

	if (szIn)
	{
		wchar_t* wszUTF16;
		int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)szIn, -1, NULL, 0);
		wszUTF16 = new wchar_t[len + 1];
		memset(wszUTF16, 0, len * 2 + 2);
		MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)szIn, -1, (LPWSTR)wszUTF16, len);
		strResult = wstring(wszUTF16);
		delete[]wszUTF16;
	}

	return strResult;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值