Windows下ANSI与UTF8之间转换,wstring与string之间的转换代码片段

Windows下ANSI与UTF8之间转换,wstring与string之间的转换代码片段

参考:multiByteToWideChar 函数 (stringapiset.h) - Win32 apps | Microsoft Learn

static std::string UTF82ANSI(const char* utf8)
{
	std::string str;
#if _WIN32
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	if (len <= 0)
		return str;
	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* buf = new char[len + 1];
	memset(buf, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, buf, len, NULL, NULL);

	str.assign(buf, len - 1);
	delete[] wstr;
	delete[] buf;
#else
	str = utf8;
#endif
	return std::move(str);
}

static std::string ANSI2UTF8(const char* gb2312)
{
	std::string str;
#if _WIN32
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	if (len <= 0)
		return str;
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* buf = new char[len + 1];
	memset(buf, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, buf, len, NULL, NULL);
	
	str.assign(buf, len - 1);
	delete[] wstr;
	delete[] buf;
#else
	str = gb2312;
#endif
	return std::move(str);
}

wstring与string直接转换

windows版

static std::wstring to_wide_string(std::string& strascii)
{
#ifdef _MSC_VER
	int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);	
	if (widesize == ERROR_NO_UNICODE_TRANSLATION)
	{
		throw std::exception("Invalid UTF-8 sequence.");
	}
	if (widesize == 0)
	{
		throw std::exception("Error in conversion.");
	}
	std::vector<wchar_t> resultstring(widesize);
	int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
	if (convresult != widesize)
	{
		throw std::exception("La falla!");
	}

	return std::wstring(&resultstring[0]);
#endif
}

static std::string to_byte_string(const std::wstring& widestring)
{ 
#ifdef _MSC_VER
	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
	if (utf8size == 0)
	{
		throw std::exception("Error in conversion.");
	}
	std::vector<char> resultstring(utf8size);

	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);

	if (convresult != utf8size)
	{
		throw std::exception("La falla!");
	}

	return std::string(&resultstring[0]);
#endif
}

C++11版

#include <string>
#include <locale>
#include <codecvt>

// convert string to wstring
inline std::wstring to_wide_string(const std::string& input)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
	return converter.from_bytes(input);
}
// convert wstring to string 
inline std::string to_byte_string(const std::wstring& input)
{
	std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
	return converter.to_bytes(input);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值