字节转换

char *gyarmyWideCharToAnsi(wchar_t *pWideChar)
{
	if (!pWideChar) return NULL;
	char *pszBuf = NULL;
	int needBytes = WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, NULL, 0, NULL, NULL);
	if (needBytes > 0){
		pszBuf = new char[needBytes + 1];
		ZeroMemory(pszBuf, (needBytes + 1)*sizeof(char));
		WideCharToMultiByte(CP_ACP, 0, pWideChar, -1, pszBuf, needBytes, NULL, NULL);
	}

	return pszBuf;
}

wchar_t *gyarmyAnsiCharToWide(char *pChar)
{
	if (!pChar) return NULL;
	wchar_t *pszBuf = NULL;
	int needWChar = MultiByteToWideChar(CP_ACP, 0, pChar, -1, NULL, 0);
	if (needWChar > 0){
		pszBuf = new wchar_t[needWChar + 1];
		ZeroMemory(pszBuf, (needWChar + 1)*sizeof(wchar_t));
		MultiByteToWideChar(CP_ACP, 0, pChar, -1, pszBuf, needWChar);
	}

	return pszBuf;
}

##更简单的写法
char *p = "abcdef";
 USES_CONVERSION;
 wchar_t *pw = A2W(p);

原文链接: 字节转换 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-254.html )

发表评论

0则评论给“字节转换”