收藏 分销(赏)

C++中字符串以及数据类型的转换方法汇总【帖子汇编】.doc

上传人:xrp****65 文档编号:7690937 上传时间:2025-01-12 格式:DOC 页数:43 大小:245.50KB 下载积分:10 金币
下载 相关 举报
C++中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第1页
第1页 / 共43页
C++中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第2页
第2页 / 共43页


点击查看更多>>
资源描述
v C++中数据类型的转换【帖子汇编】 在C++中有很多的数据类型,在编程的时候,对数据类型进行转换很常遇到的,我在编程过程中频繁遇到甚至头疼,于是在网上网罗各种方法帖子。因为时间有限没法一一整理,遂将各帖汇集一处,与大家共享。希望对大家有帮助。 为方便大家区分各帖。每个帖的首页都是另起一页,不会直接跟在后一个帖后面。 1、如何将CString变量转换为int变量 2、如何将int变量转换CString为变量 CString ss="1212.12";  int temp=atoi(ss);  CString aa;  aa.Format("%d",temp);  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 test";   或在已定义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 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),系统编译器将会自动对其进行转换。 方法三,使用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, "comsupp.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;  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 strVar("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;    方法五,使用ConvertStringToBSTR。例如: 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转换成CString   一般可按下列方法进行: 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转换成“一般”类型字符串,使用“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表示常量,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);   Visual C++ 如何:在各种字符串类型之间进行转换 本主题演示如何将各种 C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。 从 char * 转换 示例 说明 此示例演示如何从 char * 转换为上面列出的其他字符串类型。 // convert_from_char.cpp // compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() { char *orig = "Hello, World!"; 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_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) { 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 += " (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! (char *) Hello, World! (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 <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() { wchar_t *orig = L"Hello, World!"; wcout << orig << L" (wchar_t *)" << endl; // Convert to a char* 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 _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) { CW2A printstr(ccombstr); cout << printstr << endl; } // 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 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) Hello, World! (basic_string) Hello, World! (System::String) 从 _bstr_t 转换 示例 说明 此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。 // convert_from_bstr_t.cpp // compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "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); 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 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; // 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::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 转换为上面列出的其他字符串类型。 // convert_from_ccombstr.cpp // compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" #include "vcclr.h" using namespace std; using namespace System; using namespace 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(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 _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" (basic_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! (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 <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() { CString orig("Hello, World!"); wcout << orig << " (CString)" << endl; // Convert to a char* const size_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 convertedChars = 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 << 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); basicstring += " (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) 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.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() { string orig("Hello, World!"); cout << orig << " (basic_string)" << endl; // Convert 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 origsize = 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 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; } // 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}", systemstring); 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 转换为上面列出的其他字符串类型。 // convert_from_system_string.cpp // compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" #include "vcclr.h" using namespace std; using namespace System; using namespace System::Runtime::InteropServices; int main() { String ^orig = gcnew String("Hello, World!"); Console::WriteLine("{0} (System::String)", orig); pin_ptr<const wchar_t> wch = PtrToStringChars(orig); // Convert to a char* size_t origsize = wcslen(wch) + 1; const size_t
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服