1. 程式人生 > >windows下 GBKToUTF8編碼轉換

windows下 GBKToUTF8編碼轉換

char* TcpHandler::GBKToUTF8( const char* chGBK )
{
	DWORD dWideBufSize=MultiByteToWideChar(CP_ACP, 0,(LPCSTR)chGBK,-1, NULL, 0);  
	wchar_t * pWideBuf=new wchar_t[dWideBufSize];  
	wmemset(pWideBuf, 0, dWideBufSize);  
	MultiByteToWideChar(CP_ACP,0,(LPCSTR)chGBK,-1,pWideBuf,dWideBufSize);

	DWORD dUTF8BufSize=WideCharToMultiByte(CP_UTF8,0,(LPCWSTR)pWideBuf,-1,NULL,0,NULL,NULL); 

	char * pUTF8Buf=new char[dUTF8BufSize];  
	memset(pUTF8Buf, 0, dUTF8BufSize);  
	WideCharToMultiByte( CP_UTF8,0,(LPCWSTR)pWideBuf,-1,pUTF8Buf,dUTF8BufSize,NULL,NULL);

	delete[]pWideBuf;
	return pUTF8Buf;
}