• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            doing5552

            記錄每日點(diǎn)滴,不枉人生一世

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              73 Posts :: 0 Stories :: 94 Comments :: 0 Trackbacks

            公告

            常用鏈接

            留言簿(24)

            我參與的團(tuán)隊(duì)

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 454951
            • 排名 - 48

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            一、其它數(shù)據(jù)類型轉(zhuǎn)換為字符串

            • 短整型(int)
              itoa(i,temp,10);///將i轉(zhuǎn)換為字符串放入temp中,最后一個(gè)數(shù)字表示十進(jìn)制
              itoa(i,temp,2); ///按二進(jìn)制方式轉(zhuǎn)換
            • 長(zhǎng)整型(long)
              ltoa(l,temp,10);
            • 浮點(diǎn)數(shù)(float,double)
              用fcvt可以完成轉(zhuǎn)換,這是MSDN中的例子:
              int decimal, sign;
              char *buffer;
              double source = 3.1415926535;
              buffer = _fcvt( source, 7, &decimal, &sign );
              運(yùn)行結(jié)果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0
              decimal表示小數(shù)點(diǎn)的位置,sign表示符號(hào):0為正數(shù),1為負(fù)數(shù)
            • CString變量
              str = "2008北京奧運(yùn)";
              buf = (LPSTR)(LPCTSTR)str;
            • BSTR變量
              BSTR bstrValue = ::SysAllocString(L"程序員");
              char * buf = _com_util::ConvertBSTRToString(bstrValue);
              SysFreeString(bstrValue);
              AfxMessageBox(buf);
              delete(buf);
            • CComBSTR變量
              CComBSTR bstrVar("test");
              char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str);
              AfxMessageBox(buf);
              delete(buf);
            • _bstr_t變量
              _bstr_t類型是對(duì)BSTR的封裝,因?yàn)橐呀?jīng)重載了=操作符,所以很容易使用
              _bstr_t bstrVar("test");
              const char *buf = bstrVar;///不要修改buf中的內(nèi)容
              AfxMessageBox(buf);
            • 通用方法(針對(duì)非COM數(shù)據(jù)類型)
              用sprintf完成轉(zhuǎn)換
              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);
              二、字符串轉(zhuǎn)換為其它數(shù)據(jù)類型
              strcpy(temp,"123");
            • 短整型(int)
              i = atoi(temp);
            • 長(zhǎng)整型(long)l = atol(temp);
            • 浮點(diǎn)(double)   d = atof(temp);
            • CString變量CString name = temp;
            • BSTR變量BSTR bstrValue = ::SysAllocString(L"程序員");

                     ...///完成對(duì)bstrValue的使用          SysFreeString(bstrValue);

            • CComBSTR變量CComBSTR類型變量可以直接賦值 CComBSTR bstrVar1("test"); CComBSTR bstrVar2(temp);
            • _bstr_t變量_bstr_t類型的變量可以直接賦值 _bstr_t bstrVar1("test");
              _bstr_t bstrVar2(temp);

            三、其它數(shù)據(jù)類型轉(zhuǎn)換到CString使用CString的成員函數(shù)Format來轉(zhuǎn)換,例如:

            • 整數(shù)(int) str.Format("%d",i);
            • 浮點(diǎn)數(shù)(float) str.Format("%f",i);
            • 字符串指針(char *)等已經(jīng)被CString構(gòu)造函數(shù)支持的數(shù)據(jù)類型可以直接賦值 str = username;
            • 對(duì)于Format所不支持的數(shù)據(jù)類型,可以通過上面所說的關(guān)于其它數(shù)據(jù)類型轉(zhuǎn)化到char *的方法先轉(zhuǎn)到char *,然后賦值給CString變量。

            四、BSTR、_bstr_t與CComBSTR

            • CComBSTR 是ATL對(duì)BSTR的封裝,_bstr_t是C++對(duì)BSTR的封裝,BSTR是32位指針,但并不直直指向字串的緩沖區(qū)。 char *轉(zhuǎn)換到BSTR可以這樣: BSTR b=_com_util::ConvertStringToBSTR("數(shù)據(jù)");///使用前需要加上comutil.h和comsupp.lib SysFreeString(bstrValue); 反之可以使用 char *p=_com_util::ConvertBSTRToString(b); delete p; 具體可以參考一,二段落里的具體說明。
              CComBSTR與_bstr_t對(duì)大量的操作符進(jìn)行了重載,可以直接進(jìn)行=,!=,==等操作,所以使用非常方便。
              特別是_bstr_t,建議大家使用它。

            五、VARIANT 、_variant_t 與 COleVariant

            VARIANT的結(jié)構(gòu)可以參考頭文件VC98\Include\OAIDL.H中關(guān)于結(jié)構(gòu)體tagVARIANT的定義。
            對(duì)于VARIANT變量的賦值:首先給vt成員賦值,指明數(shù)據(jù)類型,再對(duì)聯(lián)合結(jié)構(gòu)中相同數(shù)據(jù)類型的變量賦值,舉個(gè)例子:
            VARIANT va;
            int a=2001;
            va.vt=VT_I4;///指明整型數(shù)據(jù)
            va.lVal=a; ///賦值
            _variant_t是VARIANT的封裝類,其賦值可以使用強(qiáng)制類型轉(zhuǎn)換,其構(gòu)造函數(shù)會(huì)自動(dòng)處理這些數(shù)據(jù)類型。
            使用時(shí)需加上#include <comdef.h>
            例如:
            long l=222;
            ing i=100;
            _variant_t lVal(l);
            lVal = (long)i;

          1. COleVariant的使用與_variant_t的方法基本一樣,請(qǐng)參考如下例子:
            COleVariant v3 = "字符串", v4 = (long)1999;
            CString str =(BSTR)v3.pbstrVal;
            long i = v4.lVal;

          2. ////////////////////////////////////////////////////////////////////////////////////
            本主題演示如何將各種 C++ 字符串類型轉(zhuǎn)換為其他字符串。以轉(zhuǎn)換的字符串類型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情況下,在將字符串轉(zhuǎn)換為新類型時(shí),都會(huì)創(chuàng)建字符串的副本。對(duì)新字符串進(jìn)行的任何更改都不會(huì)影響原始字符串,反之亦然。

          3. 從 char * 轉(zhuǎn)換

          4. // convert_from_char.cpp
            // compile with /clr /link comsuppw.lib

          5. #include <iostream>
            #include <stdlib.h>
            #include <string>

          6. #include "atlbase.h"
            #include "atlstr.h"
            #include "comutil.h"

          7. using namespace std;
            using namespace System;

          8. int main()
            {
                char *orig = "Hello, World!";
                cout << orig << " (char *)" << endl;

          9.     // Convert to a wchar_t*
                size_t rigsize = 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;
            }

          15. 輸出
            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)

          16. 從 wchar_t * 轉(zhuǎn)換
            // convert_from_wchar_t.cpp
            // compile with /clr /link comsuppw.lib

          17. #include <iostream>
            #include <stdlib.h>
            #include <string>

          18. #include "atlbase.h"
            #include "atlstr.h"
            #include "comutil.h"

          19. using namespace std;
            using namespace System;

          20. int main()
            {
                wchar_t *orig = L"Hello, World!";
                wcout << orig << L" (wchar_t *)" << endl;

          21.     // Convert to a char*
                size_t rigsize = 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;

          22.     // Convert to a _bstr_t
                _bstr_t bstrt(orig);
                bstrt += " (_bstr_t)";
                cout << bstrt << endl;

          23.     // Convert to a CComBSTR
                CComBSTR ccombstr(orig);
                if (ccombstr.Append(L" (CComBSTR)") == S_OK)
                {
                    CW2A printstr(ccombstr);
                    cout << printstr << endl;
                }

          24.     // Convert to a CString
                CString cstring(orig);
                cstring += " (CString)";
                cout << cstring << endl;

          25.     // Convert to a basic_string
                wstring basicstring(orig);
                basicstring += L" (basic_string)";
                wcout << basicstring << endl;

          26.     // Convert to a System::String
                String ^systemstring = gcnew String(orig);
                systemstring += " (System::String)";
                Console::WriteLine("{0}", systemstring);
                delete systemstring;
            }

          27. 輸出
            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 轉(zhuǎn)換
              // 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 轉(zhuǎn)換

              // 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 轉(zhuǎn)換

            // 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 rigsize = 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 轉(zhuǎn)換

            #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 rigsize = 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 轉(zhuǎn)換

            // 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 rigsize = 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;

                // Convert to a wchar_t*
                wchar_t wcstring[newsize];
                wcscpy_s(wcstring, wch);
                wcscat_s(wcstring, L" (wchar_t *)");
                wcout << wcstring << endl;

                // Convert to a _bstr_t
                _bstr_t bstrt(wch);
                bstrt += " (_bstr_t)";
                cout << bstrt << endl;

                // Convert to a CComBSTR
                CComBSTR ccombstr(wch);
                if (ccombstr.Append(L" (CComBSTR)") == S_OK)
                {
                    CW2A printstr(ccombstr);
                    cout << printstr << endl;
                }

                // Convert to a CString
                CString cstring(wch);
                cstring += " (CString)";
                cout << cstring << endl;

                // Convert to a basic_string
                wstring basicstring(wch);
                basicstring += L" (basic_string)";
                wcout << basicstring << endl;

                delete orig;
            }

             

          28. posted on 2009-01-03 14:31 doing5552 閱讀(522) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久99国产精品久久99小说| 久久婷婷五月综合色高清| 久久精品水蜜桃av综合天堂| 久久国产精品无| 久久天天躁狠狠躁夜夜2020| 久久综合九色综合久99| 久久久久久久久久久久中文字幕 | 五月丁香综合激情六月久久| 久久中文字幕精品| 伊人久久久AV老熟妇色| 亚洲精品tv久久久久久久久| 99久久国产宗和精品1上映| 久久久精品人妻一区二区三区蜜桃| 青青草原综合久久大伊人导航| 热RE99久久精品国产66热| 色综合久久中文字幕综合网| 伊色综合久久之综合久久| 2020国产成人久久精品| 亚洲精品乱码久久久久久| 久久久av波多野一区二区| 久久这里只有精品首页| 久久久国产精品| 婷婷久久五月天| 久久棈精品久久久久久噜噜| 亚洲天堂久久精品| 国内精品伊人久久久久妇| 久久人人爽人人爽人人片AV不 | 久久伊人影视| 一本色道久久综合狠狠躁 | 四虎国产永久免费久久| 久久精品国产一区二区三区| 久久人人爽人人人人片av| 久久精品www| 一本一道久久a久久精品综合| 久久精品欧美日韩精品| 久久伊人五月天论坛| 99国产精品久久| 少妇人妻综合久久中文字幕| 久久最新精品国产| 亚洲中文字幕无码久久精品1| 国产精品久久久久一区二区三区|