Visual C++ 如何:在各種字串類型之間進行轉換

來源:互聯網
上載者:User

本主題示範如何將各種 C++ 字串類型轉換為其他字串。可以轉換的字串類型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情況下,在將字串轉換為新類型時,都會建立字串的副本。對新字串進行的任何更改都不會影響原始字串,反之亦然。

從 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 CComBSTRCComBSTR ccombstr(orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;}// Convert to a CStringCString cstring(orig);cstring += " (CString)";cout << cstring << endl;// Convert to a basic_stringstring basicstring(orig);basicstring += " (basic_string)";cout << basicstring << endl;// Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;}

 

 

從 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 CComBSTRCComBSTR ccombstr(orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;}// Convert to a CStringCString cstring(orig);cstring += " (CString)";cout << cstring << endl;// Convert to a basic_stringwstring basicstring(orig);basicstring += L" (basic_string)";wcout << basicstring << endl;// Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;}

 

從 _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 CComBSTRCComBSTR ccombstr((char *)orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;}// Convert to a CStringCString cstring((char *)orig);cstring += " (CString)";cout << cstring << endl;// Convert to a basic_stringstring basicstring((char *)orig);basicstring += " (basic_string)";cout << basicstring << endl;// Convert to a System::StringString ^systemstring = gcnew String((char *)orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;}

 

 

從 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 CComBSTRCComBSTR ccombstr(orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;}// Convert to a basic_stringstring basicstring(orig);basicstring += " (basic_string)";cout << basicstring << endl;// Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;}

 

關於函數:wcstombs_s, _wcstombs_s_l 

函數描述:Converts a sequence of wide characters to a corresponding sequence of multibyte characters.

errno_t wcstombs_s(   size_t *pReturnValue,   char *mbstr,   size_t sizeInBytes,   const wchar_t *wcstr,   size_t count );errno_t _wcstombs_s_l(   size_t *pReturnValue,   char *mbstr,   size_t sizeInBytes,   const wchar_t *wcstr,   size_t count,   _locale_t locale);

 

Parameters

[out] pReturnValueThe number of characters converted.[out] mbstrThe address of a buffer for the resulting converted multibyte character string.[in] sizeInBytesThe size in bytes of the mbstr buffer.[in] wcstrPoints to the wide character string to be converted.[in] countThe maximum number of bytes to be stored in the mbstr buffer, or _TRUNCATE.[in] localeThe locale to use.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.