字符串的相互转换(TCHAR,char,CString)

/************ tchar, char, CSting转换成int************//
TCHAR tchar[MAX_PATH] = _T("123456") ;
char _char[MAX_PATH] = "123" ;
CString m_str, n_str ;
int nItem1, nItem2 ;
nItem1 = _ttoi( tchar) ; //TCHAR 转 int
nItem2 = atoi(_char) ;   //char 转 int

sprintf( _char, "%d", nItem1) ; // int 转char
m_str = _char ;
n_str.Format(_T("%d"), nItem2) ; //int 转CString



/************ tchar, char, 转换 CSting************//

        TCHAR tchar1[MAX_PATH] = _T("新MZD$123") ;
TCHAR tchar2[MAX_PATH] = {0} ;
char strchar1[MAX_PATH] = {0} ;
char strchar2[MAX_PATH] = {0} ;
CString m_str1 = _T("") ;
CString m_str2 = _T("") ;
wchar_t wszHashNew[41];
int iLength ; 


//TCHAR 转char 转CString
//获取字节长度   
iLength = WideCharToMultiByte(CP_ACP, 0, tchar1, -1, NULL, 0, NULL, NULL) ; 
//将tchar值赋给_char    
WideCharToMultiByte(CP_ACP, 0, tchar1, -1, strchar1, iLength, NULL, NULL) ;
m_str1= strchar1 ;
m_str2 = tchar1 ;


m_str1 = _T("123$MZD新") ;
USES_CONVERSION;
strcpy( (LPSTR)strchar2,OLE2A(m_str1.LockBuffer())) ; //CString 转char
lstrcpy(tchar1, m_str1.GetBuffer()); //CSting 转TCHAR

m_str2 = strchar2 ;
/*MessageBox(m_str2) ;*/
CString str = _T("") ;
str = tchar1 ;
/*MessageBox(str) ;*/


//char转TCHAR
//strcpy( strchar2, strchar1) ;
//获取字节长度   
iLength = MultiByteToWideChar (CP_ACP, 0, strchar2, strlen(strchar2) +1, NULL, 0) ; 
//将tchar值赋给_char    
MultiByteToWideChar (CP_ACP, 0, strchar2, strlen(strchar2) +1, tchar2, iLength) ;

m_str1= strchar2 ;
m_str2 = tchar2 ;
//MessageBox(m_str1) ;
//MessageBox(m_str2) ;

你可能感兴趣的:(MFC)