C++变量类型转换

1:int转换为CString

CString str;
str.Format("As string: %d", int);

2:double转换为CString

CString str;
str.Format("As string: %g", double);

3:string转换为CString

CString str
std::string ss= "Hello,World!";
str= ss.c_str();
std::string s("Hello");
CString cs(s.c_str());

 4:CString转换为string

CString cs("Hello");
std::string s((LPCTSTR)cs);

//如果是UNICODE
CString cs ("Hello");
// 转换TCHAR string为 LPCSTR
CT2CA pszConvertedAnsiString (cs);
// 用LPCSTR构造一个string
std::string strStd (pszConvertedAnsiString);

5:CString转换为double

CString thestring("13.37");
double d = atof(thestring).

//如果为Unicode编译, 使用_wtof():
CString thestring(L"13.37");
double d = _wtof(thestring).

//同时支持Unicode和非Unicode
CString thestring(_T("13.37"));
double d = _tstof(thestring).

6:CString转换为int

int iValue;
CString cstrValue;
int i;
int j = swscanf_s(cstrValue, _T("%d"), &i);
if (j != 1)
{
     // 未转换成功
    iValue= 0;
} 
else
{
    //转换成功
    iValue= i;
}

7:CString转换为char*

CStringA m_szlstfile; 
const size_t newsizea = (m_szlstfile.GetLength() + 1);
char *nstringa = new char[newsizea];
strcpy_s(nstringa, newsizea, m_szlstfile);

CT2CA pszConvertedAnsiString(m_szlstfile);
char *nstringa = pszConvertedAnsiString;

//const char *
const char *nstringa = m_szlstfile;

 8:转载自国外网站的CString转换整理

//UNICODE CString to std::wstring and std::string
CString cstrOrgString = L"SomeString";
//Converted Unicode CString to std::wstring
std::wstring wstring(cstrOrgString);
//Converted Unicode std::wstring to std::string
std::string strString;
strString.assign(wstring.begin(), wstring.end());

//UNICODE CString to std::string using ATL CW2A macros
CString cstrOrgString("SomeString");
std::string stdString(CW2A(cstrOrgString.GetString()));

//UNICODE CString to std::string and UTF-8 encoded
std::string stdString(CW2A(cstrOrgString.GetString(), CP_UTF8));

//Converted from std::string to CString by using CString Constructor
std::string strStdString("SomeString");
CString strCString(strStdString.c_str());

//Converted from CString to std::string using ANSI variant CStringA to convert to char*; construct from it
CString strSomeCstring("SomeString");
std::string strStdString(CStringA(strSomeCstring));

//Multibyte CStringA string to a char * string
CStringA cstrMyString("SomeString");
const size_t newsizea = (cstrMyString.GetLength() + 1);
char *nstringa = new char[newsizea];
strcpy_s(nstringa, newsizea, cstrMyString);

//Convert from CStringW to a char* string. To be safe, we allocate two bytes for each
//character in the original string, including the terminating null.
CStringW cstrMyString("SomeString");
const size_t newsizew = (cstrMyString.GetLength() + 1) * 2;
char *nstringw = new char[newsizew];
size_t convertedCharsw = 0;
wcstombs_s(&convertedCharsw, nstringw, newsizew, cstrMyString, _TRUNCATE);

//Convert CStringA to a wchar_t* wide string
CStringA cstrOrgStringA("SomeString");
size_t convertedCharsa = 0;
wchar_t *wcstring = new wchar_t[newsizea];
mbstowcs_s(&convertedCharsa, wcstring, newsizea, cstrOrgStringA, _TRUNCATE);

//Convert wide character CStringW string to a wide character wchar_t* string.
//To be safe, we allocate two bytes for each character in the original string, including the terminating null.
CStringW cstrOrgStringW("SomeString");
const size_t newsizew = (cstrOrgStringW.GetLength() + 1) * 2;
wchar_t *n2stringw = new wchar_t[newsizew];
wcscpy_s(n2stringw, newsizew, cstrOrgStringW);

//Convert to a wide character _bstr_t string from  a multibyte CStringA string.
CStringA cstrOrgStringA("SomeString");
 _bstr_t bstrtString(cstrOrgStringA);

//Convert to a wide character_bstr_t string from  a wide character CStringW string.
CStringW cstrOrgStringW("SomeString");
_bstr_t bstrtwString(cstrOrgStringW);

//Convert to a wide character CComBSTR string from  a multibyte character CStringA string.
CStringA cstrOrgStringA("SomeString");
CComBSTR ccombstr(cstrOrgStringA);

//Convert the wide character string to multibyte  for printing.
CW2A printstr(ccombstr);
cout << printstr << endl;

//Convert to a wide character CComBSTR string from  a wide character CStringW string.
CStringW cstrOrgStringW("SomeString");
CComBSTR ccombstrw(cstrOrgStringW);

//Convert the wide character string for printing
CW2A printstrw(ccombstrw);
wcout << printstrw << endl;

//Convert a multibyte character CStringA to a  multibyte version of a basic_string string (std::string).
CStringA cstrOrgStringA("SomeString");
std::string basicstring(cstrOrgStringA);

//Convert a wide character CStringW to a wide character version of a basic_string (std::wstring) string.
std::wstring basicstringw(cstrOrgStringW);

//Convert a multibyte character CStringA to a  System::String.
CStringA cstrOrgStringA("SomeString");
String ^systemstring = gcnew String(cstrOrgStringA);
delete systemstring;

//Convert a wide character CStringW to a System::String.
CStringW cstrOrgStringW("SomeString");
String^systemstringw = gcnew String(cstrOrgStringW);
delete systemstringw;
View Code

 

 

待续..

posted @ 2016-10-09 11:35  DeanKast  阅读(3962)  评论(0编辑  收藏  举报
Bolg of 冯广乐