|
Posted on 2008-12-05 19:08 Thinkfeng 阅读(410) 评论(0) 编辑 收藏 引用
1 int i = 100; 2 long l = 2001; 3 float f=300.2; 4 double d=12345.119; 5 char username[]="程佩君"; 6 char temp[200]; 7 char *buf; 8 CString str; 9 _variant_t v1; 10 _bstr_t v2; 11![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
一、其它数据类型转换为字符串
1 短整型(int) 2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) itoa(i,temp,10);/**////将i转换为字符串放入temp中,最后一个数字表示十进制 3![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) itoa(i,temp,2); /**////按二进制方式转换 4 长整型(long) 5 ltoa(l,temp,10); 6 浮点数(float,double) 7 用fcvt可以完成转换,这是MSDN中的例子: 8 int decimal, sign; 9 char *buffer; 10 double source = 3.1415926535; 11 buffer = _fcvt( source, 7, &decimal, &sign ); 12 运行结果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0 13 decimal表示小数点的位置,sign表示符号:0为正数,1为负数 14 CString变量 15 str = "2008北京奥运"; 16 buf = (LPSTR)(LPCTSTR)str; 17 BSTR变量 18 BSTR bstrValue = ::SysAllocString(L"程序员"); 19 char * buf = _com_util::ConvertBSTRToString(bstrValue); 20 SysFreeString(bstrValue); 21 AfxMessageBox(buf); 22 delete(buf); 23 CComBSTR变量 24 CComBSTR bstrVar("test"); 25 char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str); 26 AfxMessageBox(buf); 27 delete(buf); 28![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 29 _bstr_t变量 30 _bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用 31 _bstr_t bstrVar("test"); 32![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) const char *buf = bstrVar;/**////不要修改buf中的内容 33 AfxMessageBox(buf); 34![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 35![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 36 通用方法(针对非COM数据类型) 37 用sprintf完成转换 38![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 39 char buffer[200]; char c = '1'; int i = 35; long j = 1000; float f = 1.7320534f; sprintf( buffer, "%c",c); sprintf( buffer, "%d",i); sprintf( buffer, "%d",j); sprintf( buffer, "%f",f); 40![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
二、字符串转换为其它数据类型
1 strcpy(temp,"123"); 2![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 3 短整型(int) 4 i = atoi(temp); 5 长整型(long) 6 l = atol(temp); 7 浮点(double) 8 d = atof(temp); 9 CString变量 10 CString name = temp; 11 BSTR变量 12 BSTR bstrValue = ::SysAllocString(L"程序员"); 13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) /**////完成对bstrValue的使用14 SysFreeString(bstrValue); 15![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 16 CComBSTR变量 17 CComBSTR类型变量可以直接赋值 18 CComBSTR bstrVar1("test"); 19 CComBSTR bstrVar2(temp); 20![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 21 _bstr_t变量 22 _bstr_t类型的变量可以直接赋值 23 _bstr_t bstrVar1("test"); 24 _bstr_t bstrVar2(temp); 25![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
三、其它数据类型转换到CString
1 使用CString的成员函数Format来转换,例如: 2![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 3![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 4 整数(int) 5 str.Format("%d",i); 6 浮点数(float) 7 str.Format("%f",i); 8 字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值 9 str = username; 10 对于Format所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给CString变量。
四、BSTR、_bstr_t与CComBSTR
1 CComBSTR 是ATL对BSTR的封装,_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。 2 char *转换到BSTR可以这样: 3![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) BSTR b=_com_util::ConvertStringToBSTR("数据");/**////使用前需要加上comutil.h和comsupp.lib 4 SysFreeString(bstrValue); 5 反之可以使用 6 char *p=_com_util::ConvertBSTRToString(b); 7 delete p; 8 具体可以参考一,二段落里的具体说明。 9![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 10 CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行=,!=,==等操作,所以使用非常方便。 11 特别是_bstr_t,建议大家使用它。
五、VARIANT 、_variant_t 与 COleVariant
1 VARIANT的结构可以参考头文件VC98\Include\OAIDL.H中关于结构体tagVARIANT的定义。 2 对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子: 3 VARIANT va; 4 int a=2001; 5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) va.vt=VT_I4;/**////指明整型数据 6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) va.lVal=a; /**////赋值 7 8 9 _variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。 10 使用时需加上#include <comdef.h> 11 例如: 12 long l=222; 13 ing i=100; 14 _variant_t lVal(l); 15 lVal = (long)i; 16![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 17 COleVariant的使用与_variant_t的方法基本一样,请参考如下例子: 18 COleVariant v3 = "字符串", v4 = (long)1999; 19 CString str =(BSTR)v3.pbstrVal; 20 long i = v4.lVal; 21![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 22![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 23 本主题演示如何将各种 C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。 24![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 25![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
从 char * 转换
1 //此示例演示如何从 char * 转换为上面列出的其他字符串类型。 2 // convert_from_char.cpp 3 // compile with /clr /link comsuppw.lib 4 #include <iostream> 5 include <stdlib.h>#include <string> 6 #include "atlbase.h"#include "atlstr.h"#include "comutil.h" 7 using namespace std;using namespace System; 8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int main() { char *orig = "Hello, World!"; cout << orig << " (char *)" << endl; 9 // 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; 10 // Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)"; cout << bstrt << endl; 11 // Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } 12 // Convert to a CString CString cstring(orig); cstring += " (CString)"; cout << cstring << endl; 13 // Convert to a basic_string string basicstring(orig); basicstring += " (basic_string)"; cout << basicstring << endl; 14 // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;}
1 //输出 2 Hello, World! (char *) 3 Hello, World! (wchar_t *) 4 Hello, World! (_bstr_t) 5 Hello, World! (CComBSTR) 6 Hello, World! (CString) 7 Hello, World! (basic_string) 8 Hello, World! (System::String)
从 wchar_t * 转换
1 此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。 2![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 3 // convert_from_wchar_t.cpp// compile with /clr /link comsuppw.lib 4 #include <iostream>#include <stdlib.h>#include <string> 5 #include "atlbase.h"#include "atlstr.h"#include "comutil.h" 6 using namespace std;using namespace System; 7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int main() { wchar_t *orig = L"Hello, World!"; wcout << orig << L" (wchar_t *)" << endl; 8 // 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; 9 // Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)"; cout << bstrt << endl; 10 // Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } 11 // Convert to a CString CString cstring(orig); cstring += " (CString)"; cout << cstring << endl; 12 // Convert to a basic_string wstring basicstring(orig); basicstring += L" (basic_string)"; wcout << basicstring << endl; 13 // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;} 14 //输出 15 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)
1 从 _bstr_t 转换 2 3![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 4 示例 5![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 6 说明 7 此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。 8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 9 // convert_from_bstr_t.cpp// compile with /clr /link comsuppw.lib 10 #include <iostream>#include <stdlib.h>#include <string> 11 #include "atlbase.h"#include "atlstr.h"#include "comutil.h" 12 using namespace std;using namespace System; 13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int main() { _bstr_t orig("Hello, World!"); wcout << orig << " (_bstr_t)" << endl; 14 // Convert to a char* const size_t newsize = 100; char nstring[newsize]; strcpy_s(nstring, (char *)orig); strcat_s(nstring, " (char *)"); cout << nstring << endl; 15 // Convert to a wchar_t* wchar_t wcstring[newsize]; wcscpy_s(wcstring, (wchar_t *)orig); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; 16 // Convert to a CComBSTR CComBSTR ccombstr((char *)orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } 17 // Convert to a CString CString cstring((char *)orig); cstring += " (CString)"; cout << cstring << endl; 18 // Convert to a basic_string string basicstring((char *)orig); basicstring += " (basic_string)"; cout << basicstring << endl; 19 // Convert to a System::String String ^systemstring = gcnew String((char *)orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;} 输出 20 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) 21 从 CComBSTR 转换 22 23![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 24 示例 25![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 26 说明 27 此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。 28![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 29 // convert_from_ccombstr.cpp// compile with /clr /link comsuppw.lib 30 #include <iostream>#include <stdlib.h>#include <string> 31 #include "atlbase.h"#include "atlstr.h"#include "comutil.h"#include "vcclr.h" 32 using namespace std;using namespace System;using namespace System::Runtime::InteropServices; 33![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int main() { CComBSTR orig("Hello, World!"); CW2A printstr(orig); cout << printstr << " (CComBSTR)" << endl; 34 // 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; 35 // Convert to a wchar_t* wchar_t wcstring[newsize]; wcscpy_s(wcstring, orig); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; 36 // Convert to a _bstr_t_bstr_t bstrt(orig); bstrt += " (_bstr_t)"; cout << bstrt << endl; 37 // Convert to a CString CString cstring(orig); cstring += " (CString)"; cout << cstring << endl; 38 // Convert to a basic_string wstring basicstring(orig); basicstring += L" (basic_string)"; wcout << basicstring << endl; 39 // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;} 输出 40 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) 41 从 CString 转换 42 43![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 44 示例 45![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 46 说明 47 此示例演示如何从 CString 转换为上面列出的其他字符串类型。 48![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 49 // convert_from_cstring.cpp// compile with /clr /link comsuppw.lib 50 #include <iostream>#include <stdlib.h>#include <string> 51 #include "atlbase.h"#include "atlstr.h"#include "comutil.h" 52 using namespace std;using namespace System; 53![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int main() { CString orig("Hello, World!"); wcout << orig << " (CString)" << endl; 54 // Convert to a char* const size_t newsize = 100; char nstring[newsize]; strcpy_s(nstring, orig); strcat_s(nstring, " (char *)"); cout << nstring << endl; 55 // 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; 56 // Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)"; cout << bstrt << endl; 57 // Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } 58 // Convert to a basic_string string basicstring(orig); basicstring += " (basic_string)"; cout << basicstring << endl; 59 // Convert to a System::String String ^systemstring = gcnew String(orig); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;} 输出 60 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) 61 从 basic_string 转换 62 63![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 64 示例 65![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 66 说明 67 此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。 68![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 69 // convert_from_basic_string.cpp// compile with /clr /link comsuppw.lib 70 #include <iostream>#include <stdlib.h>#include <string> 71 #include "atlbase.h"#include "atlstr.h"#include "comutil.h" 72 using namespace std;using namespace System; 73![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int main() { string orig("Hello, World!"); cout << orig << " (basic_string)" << endl; 74 // 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; 75 // 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; 76 // Convert to a _bstr_t _bstr_t bstrt(orig.c_str()); bstrt += " (_bstr_t)"; cout << bstrt << endl; 77 // Convert to a CComBSTR CComBSTR ccombstr(orig.c_str()); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } 78 // Convert to a CString CString cstring(orig.c_str()); cstring += " (CString)"; cout << cstring << endl; 79 // Convert to a System::String String ^systemstring = gcnew String(orig.c_str()); systemstring += " (System::String)"; Console::WriteLine("{0}", systemstring); delete systemstring;}输出 80 81![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 82 83![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 84 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) 85 从 System::String 转换 86 87![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 88 示例 89![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 90 说明 91 此示例演示如何从 System.String 转换为上面列出的其他字符串类型。 92![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 93 // convert_from_system_string.cpp// compile with /clr /link comsuppw.lib 94 #include <iostream>#include <stdlib.h>#include <string> 95 #include "atlbase.h"#include "atlstr.h"#include "comutil.h"#include "vcclr.h" 96 using namespace std;using namespace System;using namespace System::Runtime::InteropServices; 97![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int main() { String ^orig = gcnew String("Hello, World!"); Console::WriteLine("{0} (System::String)", orig); 98 pin_ptr<const wchar_t> wch = PtrToStringChars(orig); 99 // Convert to a char* size_t origsize = wcslen(wch) + 1; const size_t newsize = 100; size_t convertedChars = 0; char nstring[newsize]; wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE); strcat_s(nstring, " (char *)"); cout << nstring << endl; 100 // Convert to a wchar_t* wchar_t wcstring[newsize]; wcscpy_s(wcstring, wch); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; 101 // Convert to a _bstr_t _bstr_t bstrt(wch); bstrt += " (_bstr_t)"; cout << bstrt << endl; 102 // Convert to a CComBSTR 103![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) CComBSTR ccombstr(wch); if (ccombstr.Append(L" (CComBSTR)") == S_OK) { CW2A printstr(ccombstr); cout << printstr << endl; } 104 // Convert to a CString CString cstring(wch); cstring += " (CString)"; cout << cstring << endl; 105 // Convert to a basic_string wstring basicstring(wch); basicstring += L" (basic_string)"; wcout << basicstring << endl; 106 delete orig;} 输出 107 Hello, World! (System::String)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (CString)Hello, World! (basic_string)
|