C++字符串

时间:2021-09-11 11:26:49   收藏:0   阅读:28

C++字符串

计算宽窄字符串字节

窄字符:strlen(p_str) + 1.

宽字符:(wcslen(p_str) + 1) * sizeof(wchar_t)

宽窄字节字符串的转换

WideCharToMultiByte实现宽字节转换到窄字节

MultiByteToWideChar实现窄字节转换到宽字节

示例:

//将单字节char*转化为宽字节wchar_t*  
int  AnsiToUnicode(const char* szStr, wchar_t *szStrDst)
{
	int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0);
	if (nLen == 0)
	{
		return nLen;
	}
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, szStrDst, nLen);
	return nLen;
}
///宽字节wchar_t转为单字节
char* UnicodeToAnsi(const wchar_t* szStr)
{
	int nLen = WideCharToMultiByte(CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL);
	if (nLen == 0)
	{
		return NULL;
	}
	char* pResult = new char[nLen];
	WideCharToMultiByte(CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL);
	return pResult;
}

原文:https://www.cnblogs.com/zzr-stdio/p/15250264.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!