vs2010字符串的转换

来源:互联网 发布:ios屏幕录像软件 编辑:程序博客网 时间:2024/06/12 00:31


void ConvertGBKToUtf8(std::string& strGBK)
{
 int len=MultiByteToWideChar(CP_ACP, 0, (char*)strGBK.c_str(), -1, NULL,0);
 wchar_t * wszUtf8 = new wchar_t[len+1];
 memset(wszUtf8, 0, len * 2 + 2);
 MultiByteToWideChar(CP_ACP, 0, (char*)strGBK.c_str(), -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;               
}

void ConvertUtf8ToGBK(std::string& strUtf8)
{
 int len=MultiByteToWideChar(CP_UTF8, 0, (char*)strUtf8.c_str(), -1, NULL,0);
 wchar_t * wszGBK = new wchar_t[len+1];
 memset(wszGBK, 0, len * 2 + 2);
 MultiByteToWideChar(CP_UTF8, 0, (char*)strUtf8.c_str(), -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;
}
wstring ANSIToUnicode( const string& str )
{

 int  len = 0;
 len = str.length();
 int  unicodeLen = ::MultiByteToWideChar( CP_ACP,0,str.c_str(),-1,NULL,0 ); 

 wchar_t *  pUnicode; 
 pUnicode = new  wchar_t[unicodeLen+1]; 
 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
 ::MultiByteToWideChar( CP_ACP,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen ); 

 wstring  rt; 
 rt = ( wchar_t* )pUnicode;
 delete  pUnicode;
 return  rt; 

}

string UnicodeToANSI( const wstring& str )
{
 char*     pElementText;
 int    iTextLen;

 iTextLen = WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL );

 pElementText = new char[iTextLen + 1];
 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
 ::WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,pElementText,iTextLen,NULL,NULL );

 string strText;
 strText = pElementText;
 delete[] pElementText;

 return strText;

}


wstring UTF8ToUnicode( const string& str )
{
 int  len = 0;
 len = str.length();
 int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,0,str.c_str(),-1,NULL,0 ); 

 wchar_t *  pUnicode; 
 pUnicode = new  wchar_t[unicodeLen+1]; 

 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
 ::MultiByteToWideChar( CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen ); 


 wstring  rt; 
 rt = ( wchar_t* )pUnicode;
 delete  pUnicode;

 return  rt; 

}

string UnicodeToUTF8( const wstring& str )
{
 char*     pElementText;
 int    iTextLen;

 iTextLen = WideCharToMultiByte( CP_UTF8,0,str.c_str(),-1,NULL,0,NULL,NULL );

 pElementText = new char[iTextLen + 1];
 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
 ::WideCharToMultiByte( CP_UTF8,0,str.c_str(),-1,pElementText,iTextLen,NULL,NULL );

 string strText;
 strText = pElementText;
 delete[] pElementText;

 return strText;

}

void wcharTochar(const wchar_t *wchar, char *chr, int length)    
{    
    WideCharToMultiByte( CP_ACP, 0, wchar, -1,    
        chr, length, NULL, NULL );    
}   
char *GetBuf(CString str)
{
    CString origCString=_T("");
    origCString = str;
    wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
    size_t origsize = wcslen(wCharString) + 1;
    size_t convertedChars = 0;
    char *CharString;
    CharString=(char*)malloc(origsize);
    wcharTochar(wCharString,CharString,origsize);
    return CharString;
}

0 0
原创粉丝点击