CString和string的轉換


CString和string的轉換

//從CString轉換到string
CString str1 = "ABC";
string str2 = str1.GetBuffer();

//從string轉換到CString
string str1 = "ABC";
CString str2 = str1.c_str();

char*、TCHAR*轉換CString
CString str(****)
下面詳細寫一下其它轉換

// 
/* 
*********************************************************************** 
* 函數: TransCStringToTCHAR 
* 描述:將CString 轉換為 TCHAR* 
* 日期:
*********************************************************************** 
*/ 
TCHAR* CPublic::CString2TCHAR(CString &str) 
{ 
int iLen = str.GetLength(); 
TCHAR* szRs = new TCHAR[iLen]; 
lstrcpy(szRs, str.GetBuffer(iLen)); 
str.ReleaseBuffer(); 
return szRs; 
} 
 
/* 
*********************************************************************** 
* 函數: TCHAR2Char 
* 描述:將TCHAR* 轉換為 char* 
* 日期:
*********************************************************************** 
*/ 
char* TCHAR2char(TCHAR* tchStr) 
{ 
int iLen = 2*wcslen(tchStr);//CString,TCHAR漢字算一個字符,因此不用普通計算長度 
char* chRtn = new char[iLen+1] 
wcstombs(chRtn,tchStr,iLen+1);//轉換成功返回為非負值 
return chRtn; 
} 
 
/*
*********************************************************************** 
* 函數: char2tchar
* 描述:將 char* 轉換為 TCHAR*
* 日期:
*********************************************************************** 
*/ 
TCHAR *char2tchar(char *str)
{
int iLen = strlen(str);
TCHAR *chRtn = new TCHAR[iLen+1];
mbstowcs(chRtn, str, iLen+1); return chRtn;
}
 
/* 
*********************************************************************** 
* 函數: CString2char 
* 描述:將CString轉換為 char* 
* 日期:
*********************************************************************** 
*/ 
char* CPublic::CString2char(CString &str) 
{ 
int len = str.GetLength(); 
char* chRtn = (char*)malloc((len*2+1)*sizeof(char));//CString的長度中漢字算一個長度 
memset(chRtn, 0, 2*len+1); 
USES_CONVERSION; 
strcpy((LPSTR)chRtn,OLE2A(str.LockBuffer())); 
return chRtn; 
}

WideCharToMultiByte和MultiByteToWideChar函數的用法

支持Unicode編碼,需要多字節與寬字節之間的相互轉換

WideCharToMultiByte的代碼頁用來標記與新轉換的字符串相關的代碼頁。

MultiByteToWideChar的代碼頁用來標記與一個多字節字符串相關的代碼頁。

常用的代碼頁由CP_ACP和CP_UTF8兩個。

使用CP_ACP代碼頁就實現了ANSI與Unicode之間的轉換。

使用CP_UTF8代碼頁就實現了UTF-8與Unicode之間的轉換。

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;
// wide char to multi char
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;
// wide char to multi char
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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM