您当前的位置: 首页 >  windows

插件开发

暂无认证

  • 0浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Win32-C++-字符串编码转换-windows.h-Windows系统通用

插件开发 发布时间:2022-06-23 06:15:00 ,浏览量:0

  C++字符串编码必须处理字符串流,并处理相应的编码。否则容易产生乱码。字符串编码有几种办法, 一种是开源的libconv库(C语言库),一种是boost库(C++语言库),最后一种是windows平台库。 本文介绍最后一种,windows平台库的用法,拿来主义的读者可以直接拷贝,源代码如下:

#include 
#include 
#include 
static std::wstring AsciiToUnicode(std::string asciiStr)
{
	int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, NULL, 0);
	if (0 == widesize)
	{
		return std::wstring();
	}

	std::vector resultstring(widesize);
	int count = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, &resultstring[0], widesize);
	if (0 == count)
	{
		return std::wstring();
	}

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

static std::wstring Utf8ToUnicode(const std::string &s)
{
	if (s.length() == 0) {
		return std::wstring();
	}

	// compute the length of the buffer we'll need
	int charcount = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
	if (charcount == 0) {
		return std::wstring();
	}

	// convert
	wchar_t* buf = new wchar_t[charcount];
	MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, charcount);
	std::wstring result(buf);
	delete[] buf;
	return result;
}

static std::string  UnicodeToAscii(std::wstring unicodeStr)
{
	// compute the length of the buffer we'll need
	int charcount = WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1,
		NULL, 0, NULL, NULL);
	if (charcount == 0) {
		return std::string();
	}

	// convert
	char *buf = new char[charcount];
	WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, buf, charcount,
		NULL, NULL);

	std::string result(buf);
	delete[] buf;
	return result;
}

static std::string  UnicodeToUtf8(std::wstring unicodeStr)
{
	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL);
	if (0 == utf8size)
	{
		return std::string();
	}
	std::vector resultstr(utf8size);
	int count = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, &resultstr[0], utf8size, NULL, NULL);
	if (0 == count)
	{
		return std::string();
	}

	return std::string(&resultstr[0]);
}

static std::string AsciiToUtf8(std::string asic)
{
	return UnicodeToUtf8(AsciiToUnicode(asic));
}

static std::string Utf8ToAscii(std::string utf8)
{
	return UnicodeToAscii(Utf8ToUnicode(utf8));
}
关注
打赏
1665481431
查看更多评论
立即登录/注册

微信扫码登录

0.0396s