Visual Studio中设置字符编码转换

一、byte转int

byte一般定义为unsigned char,占一个字节,int是四个字节,所以byte转int一般是byte型的数组转int。实现如下:


int Byte2Int( BYTE* lpByte, int nLpByte )
{
	int iRetVal = lpByte[0] & 0xFF;  
	if(nLpByte > 1) iRetVal |= ((lpByte[1] << 8) & 0xFF00);  
	if(nLpByte > 2) iRetVal |= ((lpByte[2] << 16) & 0xFF0000);  
	if(nLpByte > 3) iRetVal |= ((lpByte[3] << 24) & 0xFF000000); 
	return iRetVal;
}



二、int转byte

int转byte类型的数组,需要指定byte型数组的位数,也就是说,这个函数支持将int类型分别转换成1、2、3、4位byte。

BYTE* Int2Byte( int n, int nLpByte/*=4*/ )
{
	BYTE* lpByte = new BYTE[nLpByte];
	lpByte[0] = (byte)(0xFF & n);
	if(nLpByte>1) lpByte[1] = (byte)((n & 0xFF00)>>8);
	if(nLpByte>2) lpByte[2] = (byte)((n & 0xFF0000)>>16);
	if(nLpByte>3) lpByte[3] = (byte)((n & 0xFF000000)>>24);
	return lpByte;
}



三、char*转CString

如果是string的话,可以使用c_str()。

void Char2CString(const char* data,CString & str)
{

#ifdef UNICODE	
	int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)data, -1, NULL,0);
	unsigned short * wszGBK = new unsigned short[len+1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)data, -1, reinterpret_cast(wszGBK), len); 
	str.Format(_T("%s"),wszGBK);
	delete[] wszGBK;
#else
	WCHAR* strA;
	int i= MultiByteToWideChar ( CP_UTF8 , 0 ,(char*) data ,-1 ,NULL,0);
	strA = new WCHAR[i+1];
	MultiByteToWideChar ( CP_UTF8 , 0 ,( char * ) data, -1, strA , i);
	i= WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);
	char *strB=new char[i+1];
	WideCharToMultiByte (CP_ACP,0,strA,-1,strB,i,NULL,NULL);
	str.Format(_T("%s"),strB);
	delete []strA;
	delete []strB;
	//	AfxMessageBox(str);
#endif
}



四、CString转char*


//要求pChar必须有足够的空间
void CString2Char(const CString& str,char* pChar)
{

	int len = 0;

	if (!pChar)
	{
		return;
	}

#ifdef UNICODE
	len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL); 
	memset(pChar, 0, len + 1);
	WideCharToMultiByte (CP_UTF8, 0, str, -1, pChar, len, NULL,NULL);
	return;
#else
	if (str.IsEmpty())
	{
		return NULL;
	}
	char* pStr;
	int l = str.GetLength();
	int i;
	pStr = new char[l+1];
	sprintf(pStr,_T("%s"),str);
	TRACE(_T("%s\n"),pStr);
	i= MultiByteToWideChar(CP_ACP, 0 ,(char*) pStr ,l+1 ,NULL,0);
	WCHAR *strC=new WCHAR[i];
	MultiByteToWideChar (CP_ACP, 0 ,(char*) pStr ,l+1 ,strC,i);
	i= WideCharToMultiByte(CP_UTF8,0,strC,-1,NULL,0,NULL,NULL);
	//char *strD=new char[i];
	WideCharToMultiByte (CP_UTF8,0,strC,-1,pChar,i,NULL,NULL);
	len = i-1;
	delete []pStr;
	delete []strC;
	return;
#endif

}



五、UTF8编码的字符串和CString之间互转


void ConvertUTF8ToCString(void * strUtf8 ,CString & str)
{
#ifdef UNICODE	
	int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, NULL,0);
	unsigned short * wszGBK = new unsigned short[len+1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, reinterpret_cast(wszGBK), len); 
	str.Format(_T("%s"),wszGBK);
	delete[] wszGBK;
#else
	WCHAR* strA;
	int i= MultiByteToWideChar ( CP_UTF8 , 0 ,(char*) strUtf8 ,-1 ,NULL,0);
	strA = new WCHAR[i+1];
	MultiByteToWideChar ( CP_UTF8 , 0 ,( char * ) strUtf8, -1, strA , i);
	i= WideCharToMultiByte(CP_ACP,0,strA,-1,NULL,0,NULL,NULL);
	char *strB=new char[i+1];
	WideCharToMultiByte (CP_ACP,0,strA,-1,strB,i,NULL,NULL);
	str.Format(_T("%s"),strB);
	delete []strA;
	delete []strB;
#endif
}
char*  ConvertCStringToUTF8(CString &str,int &len)
{
#ifdef UNICODE
	len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL); 
	char *szUtf8=new char[len + 1];
	memset(szUtf8, 0, len + 1);
	WideCharToMultiByte (CP_UTF8, 0, str, -1, szUtf8, len, NULL,NULL);
	len = len-1;
	return szUtf8;
#else
	if (str.IsEmpty())
	{
		return NULL;
	}
	char* pStr;
	int l = str.GetLength();
	int i;
	pStr = new char[l+1];
	sprintf(pStr,_T("%s"),str);
	TRACE(_T("%s\n"),pStr);
	i= MultiByteToWideChar(CP_ACP, 0 ,(char*) pStr ,l+1 ,NULL,0);
	WCHAR *strC=new WCHAR[i];
	MultiByteToWideChar (CP_ACP, 0 ,(char*) pStr ,l+1 ,strC,i);
	i= WideCharToMultiByte(CP_UTF8,0,strC,-1,NULL,0,NULL,NULL);
	char *strD=new char[i];
	WideCharToMultiByte (CP_UTF8,0,strC,-1,strD,i,NULL,NULL);
	len = i-1;
	delete []pStr;
	delete []strC;
	return strD;
#endif
}


你可能感兴趣的:(C/C++)