Data Type Conversions
Submitted by Date of Submission User Level
Mahesh Chand Jan 22, 2001 Beginner
From CString to WCHAR*?
CString str = "A string here" ;
LPWSTR lpszW = new WCHAR[255];
LPTSTR lpStr = str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);
AFunctionUsesWCHAR( lpszW );
delete[] lpszW;
From LPTSTR to LPWSTR?
int nLen = MultiByteToWideChar(CP_ACP, 0, lptStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lptStr, -1, lpwStr, nLen);
From string to BSTR?
string ss = "mindcracker";
BSTR _bstr_home = A2BSTR(ss.c_str());
From Char* to BSTR?
char * p = "whatever";
_bstr_t bstr = p;
From CString to BSTR?
CString str = "whatever" ;
BSTR resultsString = str.AllocSysString();
From lower to UPPER case?
char msg[] = "tesTING conVERSion";
char *p;
_cputs ( msg );
for( p = msg; p /* make all upper case of message. */ _putch( _toupper( *p ) ); /* make all lower case of message. */ _putch( _tolower( *p ) ); } From string to double, integer, or long? atof, atoi, and atol functions are used to convert from string to double, integer and long values. If you have CString then First you have to convert CString into char* by casting CString with (char*). These functions are defined into stdlib. So you have to include stdlib.h. Here is an example: #include #include char *str = " -343.23 "; double dVal; int iVal; long lVal; dVal = atof( str ); str = "7.100000000000001"; dVal = atof( str ); str = " -9999 jojo"; iVal = atoi( str ); str = "99999 bumpy"; lVal = atol( str );