1、 v C++中数据类型的转换【帖子汇编】 在C++中有很多的数据类型,在编程的时候,对数据类型进行转换很常遇到的,我在编程过程中频繁遇到甚至头疼,于是在网上网罗各种方法帖子。因为时间有限没法一一整理,遂将各帖汇集一处,与大家共享。希望对大家有帮助。 为方便大家区分各帖。每个帖的首页都是另起一页,不会直接跟在后一个帖后面。 1、如何将CString变量转换为int变量 2、如何将int变量转换CString为变量 CString ss="1212.12"; int temp=atoi(ss); CString aa; aa.Format("%d",temp);
2、 3、CString转换为Double 4、Double转换为CString CString strValue("1.234"); double dblValue; dblValue = atof((LPCTSTR)strValue); 5、char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如: char chArray[] = "This is a test"; char * p = "This is a test"; 或 LPSTR p = "This is a tes
3、t"; 或在已定义Unicode应的用程序中 TCHAR * p = _T("This is a test"); 或 LPTSTR p = _T("This is a test"); CString theString = chArray; theString.Format(_T("%s"), chArray); theString = p; 6、CString转换成char* 若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法: 方法一,使用强制转换。例如: CString theString( "This is a
4、 test" ); LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString; 方法二,使用strcpy。例如: CString theString( "This is a test" ); LPTSTR lpsz = new TCHAR[theString.GetLength()+1]; _tcscpy(lpsz, theString); 需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动
5、对其进行转换。 方法三,使用CString::GetBuffer。例如: CString s(_T("This is a test ")); LPTSTR p = s.GetBuffer(); // 在这里添加使用p的代码 if(p != NULL) *p = _T('\0'); s.ReleaseBuffer(); // 使用完后及时释放,以便能使用其它的CString成员函数 7、BSTR转换成char* 方法一,使用ConvertBSTRToString。例如: #include #pragma comment(lib, "comsup
6、p.lib") int _tmain(int argc, _TCHAR* argv[]){ BSTR bstrText = ::SysAllocString(L"Test"); char* lpszText2 = _com_util::ConvertBSTRToString(bstrText); SysFreeString(bstrText); // 用完释放 delete[] lpszText2; return 0; } 方法二,使用_bstr_t的赋值运算符重载。例如: _bstr_t b = bstrText; char* lpszText2 = b;
7、 8、char*转换成BSTR 方法一,使用SysAllocString等API函数。例如: BSTR bstrText = ::SysAllocString(L"Test"); BSTR bstrText = ::SysAllocStringLen(L"Test",4); BSTR bstrText = ::SysAllocStringByteLen("Test",4); 方法二,使用COleVariant或_variant_t。例如: //COleVariant strVar("This is a test"); _variant_t str
8、Var("This is a test"); BSTR bstrText = strVar.bstrVal; 方法三,使用_bstr_t,这是一种最简单的方法。例如: BSTR bstrText = _bstr_t("This is a test"); 方法四,使用CComBSTR。例如: BSTR bstrText = CComBSTR("This is a test"); 或 CComBSTR bstr("This is a test"); BSTR bstrText = bstr.m_str; 方法五,使用Conver
9、tStringToBSTR。例如: char* lpszText = "Test"; BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText); 9、CString转换成BSTR 通常是通过使用CStringT::AllocSysString来实现。例如: CString str("This is a test"); BSTR bstrText = str.AllocSysString(); … SysFreeString(bstrText); // 用完释放 10、BSTR转换成CSt
10、ring 一般可按下列方法进行: BSTR bstrText = ::SysAllocString(L"Test"); CStringA str; str.Empty(); str = bstrText; 或 CStringA str(bstrText); 11、ANSI、Unicode和宽字符之间的转换 方法一,使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。 方法二,使用“_T”将ANSI转换成“一般”类
11、型字符串,使用“L”将ANSI转换成Unicode,而在托管C++环境中还可使用S将ANSI字符串转换成String*对象。例如: TCHAR tstr[] = _T("this is a test"); wchar_t wszStr[] = L"This is a test"; String* str = S”This is a test”; 方法三,使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类,它具有如图3所示的统一形式: 其中,第一个C表示“类”,以便于ATL 3.0宏相区别,第二个C表示常量,
12、2表示“to”,EX表示要开辟一定大小的缓冲。SourceType和DestinationType可以是A、T、W和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。例如,CA2CT就是将ANSI转换成一般类型的字符串常量。下面是一些示例代码: LPTSTR tstr= CA2TEX<16>("this is a test"); LPCTSTR tcstr= CA2CT("this is a test"); wchar_t wszStr[] = L"This is a test"; char* chstr = CW2A(wszStr);
13、 Visual C++ 如何:在各种字符串类型之间进行转换 本主题演示如何将各种 C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。 从 char * 转换 示例 说明 此示例演示如何从 char * 转换为上面列出的其他字符串类型。 /
14、/ convert_from_char.cpp
// compile with /clr /link comsuppw.lib
#include
15、 cout << orig << " (char *)" << endl; // Convert to a wchar_t* size_t origsize = strlen(orig) + 1; const size_t newsize = 100; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); wcscat_
16、s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; // Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)"; cout << bstrt << endl; // Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK)
17、 { CW2A printstr(ccombstr); cout << printstr << endl; } // Convert to a CString CString cstring(orig); cstring += " (CString)"; cout << cstring << endl; // Convert to a basic_string string basicstring(orig); basicstring += " (ba
18、sic_string)"; cout << basicstring << endl; // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring; } 输出 Hello, World! (char *) Hello, World!
19、wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
从 wchar_t * 转换
示例
说明
此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。
// convert_from_wchar_t.cpp
// compile with /clr /link comsuppw.lib
#include 20、am>
#include 21、 size_t origsize = wcslen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
char nstring[newsize];
wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a _bs 22、tr_t
_bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
23、// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(orig);
basicstring += L" (basic_string)";
wcout << basicstring << endl;
// Convert to a System::String 24、
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
输出
Hello, World! (wchar_t *)
Hello, World! (char *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString) 25、
Hello, World! (basic_string)
Hello, World! (System::String)
从 _bstr_t 转换
示例
说明
此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。
// convert_from_bstr_t.cpp
// compile with /clr /link comsuppw.lib
#include 26、nclude "comutil.h"
using namespace std;
using namespace System;
int main()
{
_bstr_t orig("Hello, World!");
wcout << orig << " (_bstr_t)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, (char *)orig);
27、 strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, (wchar_t *)orig);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a CComBSTR
CComBSTR 28、 ccombstr((char *)orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring((char *)orig);
cstring += " (CString)";
cout << cstring << endl;
29、 // Convert to a basic_string
string basicstring((char *)orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String((char *)orig);
systemstring += " (System::String)";
Console:: 30、WriteLine("{0}", systemstring);
delete systemstring;
}
输出
Hello, World! (_bstr_t)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
从 CComBSTR 转换
示例
说明
此示例演示如何从 CComBSTR 转换为 31、上面列出的其他字符串类型。
// convert_from_ccombstr.cpp
// compile with /clr /link comsuppw.lib
#include 32、 System::Runtime::InteropServices;
int main()
{
CComBSTR orig("Hello, World!");
CW2A printstr(orig);
cout << printstr << " (CComBSTR)" << endl;
// Convert to a char*
const size_t newsize = 100;
char nstring[newsize];
CW2A tmpstr1(orig);
strcpy_s( 33、nstring, tmpstr1);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
wchar_t wcstring[newsize];
wcscpy_s(wcstring, orig);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
34、bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(orig);
basicstring += L" (bas 35、ic_string)";
wcout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
输出
Hello, World! (CComBSTR)
Hello, World 36、 (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CString)
Hello, World! (basic_string)
Hello, World! (System::String)
从 CString 转换
示例
说明
此示例演示如何从 CString 转换为上面列出的其他字符串类型。
// convert_from_cstring.cpp
// compile with /clr /link comsuppw.lib
#include 37、
#include 38、e_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, orig);
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
// You must first convert to a char * for this to work.
size_t origsize = strlen(orig) + 1;
size_t co 39、nvertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t bstrt(orig);
bstrt += " (_bstr_t)";
cout << 40、 bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a basic_string
string basicstring(orig);
basicst 41、ring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig);
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstring);
delete systemstring;
}
输出
Hello, World! (CString)
42、Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (basic_string)
Hello, World! (System::String)
从 basic_string 转换
示例
说明
此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。
// convert_from_basic_string.cpp
// compile with /clr /link comsuppw 43、lib
#include 44、vert to a char*
const size_t newsize = 100;
char nstring[newsize];
strcpy_s(nstring, orig.c_str());
strcat_s(nstring, " (char *)");
cout << nstring << endl;
// Convert to a wchar_t*
// You must first convert to a char * for this to work.
size_t origs 45、ize = strlen(orig.c_str()) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;
// Convert to a _bstr_t
_bstr_t 46、bstrt(orig.c_str());
bstrt += " (_bstr_t)";
cout << bstrt << endl;
// Convert to a CComBSTR
CComBSTR ccombstr(orig.c_str());
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// 47、Convert to a CString
CString cstring(orig.c_str());
cstring += " (CString)";
cout << cstring << endl;
// Convert to a System::String
String ^systemstring = gcnew String(orig.c_str());
systemstring += " (System::String)";
Console::WriteLine("{0}", systemstr 48、ing);
delete systemstring;
}
输出
Hello, World! (basic_string)
Hello, World! (char *)
Hello, World! (wchar_t *)
Hello, World! (_bstr_t)
Hello, World! (CComBSTR)
Hello, World! (CString)
Hello, World! (System::String)
从 System::String 转换
示例
说明
此示例演示如何从 System.String 转换为上面列出的其他字符串类型。
49、// convert_from_system_string.cpp
// compile with /clr /link comsuppw.lib
#include 50、untime::InteropServices;
int main()
{
String ^orig = gcnew String("Hello, World!");
Console::WriteLine("{0} (System::String)", orig);
pin_ptr






