直接进行转码

std::string zmq_client::Utf8ToGbk(const char *src_str)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
	wchar_t* wszGBK = new wchar_t[len + 1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
	len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
	char* szGBK = new char[len + 1];
	memset(szGBK, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
	string strTemp(szGBK);
	if (wszGBK) delete[] wszGBK;
	if (szGBK) delete[] szGBK;
	return strTemp;
}

使用Boot转码库

#include <boost/locale/encoding.hpp>

std::string zmq_client::utf8_to_gbk(const std::string & str)
{
	return boost::locale::conv::between(str, "GBK", "UTF-8");
}

推荐使用Boost转码库,更加通用,考虑也比较周全。

使用Boost库时,需要依赖libboost_locale-vc140-mt-1_64.lib这个lib。(来自于自己编译的Boost库)