多字节宽字节相互转换

梦想游戏人
目录:
C/C++

多字节指的是char* 类型 ASCII标准

宽字节指的是WCHAR *类型 Unicode标准

  • LPWSTR Utils::toUnicode(const char* _str)
  • {
  • LPWSTR _ret;
  • int _len = strlen(_str) * 2;
  • _ret = new WCHAR[_len];
  • MultiByteToWideChar(CP_ACP, 0, _str, -1, _ret, _len);
  • return _ret;
  • }
LPWSTR Utils::toUnicode(const char* _str)
{
	LPWSTR _ret;
	int _len = strlen(_str) * 2;

	_ret = new WCHAR[_len];

	MultiByteToWideChar(CP_ACP, 0, _str, -1, _ret, _len);

	return _ret;
}
  • char * Utils::toAscii(LPWSTR str)
  • {
  • char* pElementText;
  • int iTextLen;
  • // wide char to multi char
  • iTextLen = WideCharToMultiByte(CP_ACP,
  • 0,
  • str,
  • -1,
  • NULL,
  • 0,
  • NULL,
  • NULL);
  • pElementText = new char[iTextLen + 1];
  • memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1));
  • ::WideCharToMultiByte(CP_ACP,
  • 0,
  • str,
  • -1,
  • pElementText,
  • iTextLen,
  • NULL,
  • NULL);
  • string ret;
  • ret= pElementText;
  • delete[] pElementText;
  • return ret;
  • }
char *  Utils::toAscii(LPWSTR str)
{
	char*     pElementText;
	int    iTextLen;
	// wide char to multi char
	iTextLen = WideCharToMultiByte(CP_ACP,
		0,
		str,
		-1,
		NULL,
		0,
		NULL,
		NULL);
	pElementText = new char[iTextLen + 1];
	memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1));
	::WideCharToMultiByte(CP_ACP,
		0,
		str,
		-1,
		pElementText,
		iTextLen,
		NULL,
		NULL);
	string ret;
	ret= pElementText;
	delete[] pElementText;
	return ret;
}
Scroll Up