• <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>

            對有關數據類型轉換的整理

            int i = 100;
            long l = 2001;
            float f=300.2;
            double d=12345.119;
            char username[]="程佩君";
            char temp[200];
            char *buf;
            CString str;
            _variant_t v1;
            _bstr_t v2;

            一、其它數據類型轉換為字符串

            • 短整型(int)
              itoa(i,temp,10);///將i轉換為字符串放入temp中,最后一個數字表示十進制
              itoa(i,temp,2); ///按二進制方式轉換
            • 長整型(long)
              ltoa(l,temp,10);
            • 浮點數(float,double)
              用fcvt可以完成轉換,這是MSDN中的例子:
              int decimal, sign;
              char *buffer;
              double source = 3.1415926535;
              buffer = _fcvt( source, 7, &decimal, &sign );
              運行結果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0
              decimal表示小數點的位置,sign表示符號:0為正數,1為負數
            • CString變量
              str = "2008北京奧運";
              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類型是對BSTR的封裝,因為已經重載了=操作符,所以很容易使用
              _bstr_t bstrVar("test");
              const char *buf = bstrVar;///不要修改buf中的內容
              AfxMessageBox(buf);

            • 通用方法(針對非COM數據類型)
              用sprintf完成轉換
              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); 

            二、字符串轉換為其它數據類型
            strcpy(temp,"123");

            • 短整型(int)
              i = atoi(temp);
            • 長整型(long)
              l = atol(temp);
            • 浮點(double)
              d = atof(temp);
            • CString變量
              CString name = temp;
            • BSTR變量
              BSTR bstrValue = ::SysAllocString(L"程序員");
              ...///完成對bstrValue的使用
              SysFreeString(bstrValue);
            • CComBSTR變量
              CComBSTR類型變量可以直接賦值
              CComBSTR bstrVar1("test");
              CComBSTR bstrVar2(temp);
            • _bstr_t變量
              _bstr_t類型的變量可以直接賦值
              _bstr_t bstrVar1("test");
              _bstr_t bstrVar2(temp);

            三、其它數據類型轉換到CString
            使用CString的成員函數Format來轉換,例如:

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

            四、BSTR、_bstr_t與CComBSTR

            • CComBSTR 是ATL對BSTR的封裝,_bstr_t是C++對BSTR的封裝,BSTR是32位指針,但并不直接指向字串的緩沖區。
              char *轉換到BSTR可以這樣:
              BSTR b=_com_util::ConvertStringToBSTR("數據");///使用前需要加上comutil.h和comsupp.lib
              SysFreeString(bstrValue);
              反之可以使用
              char *p=_com_util::ConvertBSTRToString(b);
              delete p;
              具體可以參考一,二段落里的具體說明。

              CComBSTR與_bstr_t對大量的操作符進行了重載,可以直接進行=,!=,==等操作,所以使用非常方便。
              特別是_bstr_t,建議大家使用它。

            五、VARIANT 、_variant_t 與 COleVariant

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

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

              //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            • 本主題演示如何將各種 C++ 字符串類型轉換為其他字符串。可以轉換的字符串類型包括 char *wchar_t*_bstr_tCComBSTRCStringbasic_stringSystem.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 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;
              }

              輸出

              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)
            posted on 2006-12-14 14:22 清源游民 閱讀(2503) 評論(2)  編輯 收藏 引用 所屬分類: C++

            FeedBack:
            # re: 煩人的類型轉換
            2009-01-15 16:31 | gogoplayer
            好文!  回復  更多評論
              
            # re: 煩人的類型轉換
            2010-07-14 14:04 | 162212521@163.com
            挺詳細的 謝謝分享  回復  更多評論
              
            <2009年8月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            99精品久久精品一区二区| 国产免费久久精品99re丫y| 无码超乳爆乳中文字幕久久| 欧美久久一级内射wwwwww.| 国内精品久久久久影院薰衣草 | 久久久久久久波多野结衣高潮| 亚洲精品国产综合久久一线| 91精品国产9l久久久久| 无码精品久久久久久人妻中字| 潮喷大喷水系列无码久久精品| 久久久久久久97| 久久久久亚洲av无码专区喷水| 18禁黄久久久AAA片| 无码八A片人妻少妇久久| 中文字幕无码精品亚洲资源网久久| 超级97碰碰碰碰久久久久最新| 久久精品无码一区二区三区日韩| 国产精品99久久精品爆乳| 亚洲国产精品久久| 久久男人中文字幕资源站| 精品久久久久久国产91| 久久e热在这里只有国产中文精品99 | 久久激情五月丁香伊人| 亚洲国产精品无码久久| 大香网伊人久久综合网2020| 亚洲国产精品无码成人片久久| 香蕉99久久国产综合精品宅男自 | 91久久福利国产成人精品| 久久精品国产AV一区二区三区| 国产999精品久久久久久| 亚洲国产综合久久天堂| 国产高潮国产高潮久久久91| 久久91精品国产91久久麻豆| 国产美女久久精品香蕉69| 亚洲AV日韩AV天堂久久| 一本久久知道综合久久| 亚洲AV无码久久精品色欲| 蜜臀久久99精品久久久久久小说| 久久久久人妻精品一区二区三区| 久久亚洲精品无码aⅴ大香| 久久九色综合九色99伊人|