How does visual c ++ convert string types?

Source: Internet
Author: User

This topic demonstrates how to convert various C ++ string types to other strings. The string types that can be converted include char *, wchar_t *, _ bstr_t, ccombstr, cstring, basic_string, and system. String. In all cases, a copy of the string is created when a string is converted to a new type. Any changes made to the new string will not affect the original string, and vice versa.

Convert from 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]; convert (& 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 ;}

 

 

Convert from 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 ;}

 

Convert from _ 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 ;}

 

 

Convert from 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]; convert (& convertedchars, wcstring, origsize, orig, _ truncate, 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 ;}

 

Function: wcstombs_s, _ wcstombs_s_l

Function Description: converts a sequence of wide characters to a corresponding sequence of multibyte characters.

 
Struct (size_t * preturnvalue, char * mbstr, size_t percent, const wchar_t * wcstr, size_t count); struct _ struct (size_t * preturnvalue, char * mbstr, size_t percent, 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. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.