Loading

各类字符相互转换

一.GB2312和UTF-8互转

//UTF-8到GB2312的转换
char* U2G(const char* utf8)
{                                                                                                                      
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len+1];
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr)                           
        delete[] wstr;
    return str;
}
 
//GB2312到UTF-8的转换
char* G2U(const char* gb2312)
{
    int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
    len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len+1];
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr)                          
       delete[] wstr;
    return str;
}

二.UTF8和GBK互转

//UTF8转化为GBK格式
void ConvertUtf8ToGBK(CString &strUtf8)
{
    int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
    wchar_t * wszGBK = new wchar_t[len];
    memset(wszGBK,0,len);
    MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len); 

    len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
    char *szGBK=new char[len + 1];
    memset(szGBK, 0, len + 1);
    WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);

    strUtf8 = szGBK;
    delete[] szGBK;
    delete[] wszGBK;
}

//GBK转化为UTF8格式
void ConvertGBKToUtf8(CString &strGBK)
{
    int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
    wchar_t * wszUtf8 = new wchar_t [len];
    memset(wszUtf8, 0, len);
    MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);

    len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
    char *szUtf8=new char[len + 1];
    memset(szUtf8, 0, len + 1);
    WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);

    strGBK = szUtf8;
    delete[] szUtf8;
    delete[] wszUtf8;
}

三.UTF8和ANSI互转

//UTF8转ANSI
void UTF8toANSI(CString &strUTF8)
{
  //获取转换为多字节后需要的缓冲区大小,创建多字节缓冲区
  UINT nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8,-1,NULL,NULL);
  WCHAR *wszBuffer = new WCHAR[nLen+1];
  nLen = MultiByteToWideChar(CP_UTF8,NULL,strUTF8,-1,wszBuffer,nLen);
  wszBuffer[nLen] = 0;

  nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
  CHAR *szBuffer = new CHAR[nLen+1];
  nLen = WideCharToMultiByte(936,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
  szBuffer[nLen] = 0;
  strUTF8 = szBuffer;
  //清理内存
  delete []szBuffer;
  delete []wszBuffer;
}
//ANSI转UTF8
void ANSItoUTF8(CString &strAnsi)
{
  //获取转换为宽字节后需要的缓冲区大小,创建宽字节缓冲区,936为简体中文GB2312代码页
  UINT nLen = MultiByteToWideChar(936,NULL,strAnsi,-1,NULL,NULL);
  WCHAR *wszBuffer = new WCHAR[nLen+1];
  nLen = MultiByteToWideChar(936,NULL,strAnsi,-1,wszBuffer,nLen);
  wszBuffer[nLen] = 0;
  //获取转为UTF8多字节后需要的缓冲区大小,创建多字节缓冲区
  nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,NULL,NULL,NULL,NULL);
  CHAR *szBuffer = new CHAR[nLen+1];
  nLen = WideCharToMultiByte(CP_UTF8,NULL,wszBuffer,-1,szBuffer,nLen,NULL,NULL);
  szBuffer[nLen] = 0;
  strAnsi = szBuffer;
  //内存清理
  delete []wszBuffer;
  delete []szBuffer;
}

四.UTF-8 和 CString 互相转换

char* CreateUTF8TextInitWithString(CString strValue)
{
	char *buffer = NULL;
	int  length;
 
#ifdef _UNICODE
	length = WideCharToMultiByte(CP_UTF8, 0, strValue, -1, NULL, 0, NULL, NULL);
#else
	return NULL;
#endif
	if (length <= 0)
		return NULL;
 
	buffer = new char[length];
	if (buffer == NULL)
		return NULL;
 
	ZeroMemory(buffer, length);
 
#ifdef _UNICODE
	WideCharToMultiByte(CP_UTF8, 0, strValue, -1, buffer, length, NULL, NULL);
#else
	strcpy_s(buffer, length, strValue);
#endif
	return buffer;
}
 
 
BOOL CreateString_InitWithUTF8Text(CString& str, char* pUTF8Text)
{
	if (NULL == pUTF8Text)
		return FALSE;
 
	int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,0,pUTF8Text,-1,NULL,0); 
 
	wchar_t*  pUnicode = new  wchar_t[unicodeLen+1];
	if (NULL == pUnicode)
		return FALSE;

	MultiByteToWideChar( CP_UTF8, 0, pUTF8Text, -1, (LPWSTR)pUnicode, unicodeLen );
	str = pUnicode;
	delete []pUnicode;
	return TRUE;
}

五.string类型的utf-8字符串转为CString类型的unicode字符串

//string类型的utf-8字符串转为CString类型的unicode字符串
CString ConvertUTF8ToCString( std::string utf8str )
{
    /* 预转换,得到所需空间的大小 */
    int nLen = ::MultiByteToWideChar( CP_UTF8, NULL,
                      utf8str.data(), utf8str.size(), NULL, 0 );
    /* 转换为Unicode */
    std::wstring wbuffer;
    wbuffer.resize( nLen );
    ::MultiByteToWideChar( CP_UTF8, NULL, utf8str.data(), utf8str.size(),
                   (LPWSTR) (wbuffer.data() ), wbuffer.length() );
 
#ifdef UNICODE
    return(CString( wbuffer.data(), wbuffer.length() ) );
#else
    /*
     * 转换为ANSI
     * 得到转换后长度
     */
    nLen = WideCharToMultiByte( CP_ACP, 0,
                    wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL );
 
    std::string ansistr;
    ansistr.resize( nLen );
 
    /* 把unicode转成ansi */
    WideCharToMultiByte( CP_ACP, 0, (LPWSTR) (wbuffer.data() ), wbuffer.length(),
                 (LPSTR) (ansistr.data() ), ansistr.size(), NULL, NULL );
    return(CString( ansistr.data(), ansistr.length() ) );
#endif
}

六.TCHAR与char 互转

char* TCHAR2char(const TCHAR* tchStr)
{
	char* pDest = NULL;

#ifdef _UNICODE 
	int iSize = WideCharToMultiByte(CP_ACP, 0, tchStr, -1, NULL, 0, NULL, NULL);
	pDest = (char*)malloc(sizeof(char)*(iSize + 1));
	WideCharToMultiByte(CP_ACP, 0, tchStr, -1, pDest, iSize, NULL, NULL);
#else
	pDest = (char*)malloc(strlen(tchStr) + 1); 
	strcpy(pDest, tchStr);
#endif

	return pDest;
}

TCHAR* char2TCHAR(const char* charStr)
{
	if(charStr == NULL)
		return NULL;
	TCHAR *pDest = NULL;

#ifdef _UNICODE 
	int iSize = MultiByteToWideChar(CP_ACP, 0, charStr, -1, NULL, 0);
	pDest = (TCHAR*)malloc(sizeof(TCHAR)*(iSize + 1));
	MultiByteToWideChar(CP_ACP, 0, charStr, -1, pDest, iSize);
#else
	pDest = new TCHAR[strlen(charStr) + 1];
	strcpy(pDest, charStr);
#endif
	return pDest;
}

七.char与字符串互转

CString C2W(const char* chr)
{
	TCHAR *Dst = char2TCHAR(chr);
	CString Rst = Dst;
	free(Dst);

	return Rst;
}

std::string W2C(const wchar_t* wchr)
{
	char *Dst = TCHAR2char((LPCTSTR)wchr);
	std::string Rst = Dst;
	free(Dst);
	return Rst;
}
posted @ 2019-08-14 17:05  F(x)_King  阅读(450)  评论(0编辑  收藏  举报