Skip to content

Commit 1f88ecf

Browse files
TylerMSFTTylerMSFT
authored andcommitted
correction regarding CStringA
1 parent 4ea65ac commit 1f88ecf

1 file changed

Lines changed: 48 additions & 48 deletions

File tree

‎docs/text/how-to-convert-between-various-string-types.md‎

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
description: "Learn more about: How to: Convert Between Various String Types"
33
title: "How to: Convert Between Various String Types"
44
ms.custom: "get-started-article"
5-
ms.date: "11/04/2016"
5+
ms.date: "02/08/2021"
66
helpviewer_keywords: ["converting string types", "string conversion [C++]", "strings [C++], converting"]
77
ms.assetid: e7e4f741-3c82-45f0-b8c0-1e1e343b0e77
88
---
9-
# How to: Convert Between Various String Types
9+
# How to: Convert between various string types
1010

11-
This topic demonstrates how to convert various Visual C++ string types into other strings. The strings types that are covered include `char *`, `wchar_t*`, [_bstr_t](../cpp/bstr-t-class.md), [CComBSTR](../atl/reference/ccombstr-class.md), [CString](../atl-mfc-shared/using-cstring.md), [basic_string](../standard-library/basic-string-class.md), and <xref:System.String?displayProperty=fullName>. In all cases, a copy of the string is made when converted to the new type. Any changes made to the new string will not affect the original string, and vice versa.
11+
This topic shows how to convert various Visual C++ string types into other strings. The strings types that are covered include `char *`, `wchar_t*`, [`_bstr_t`](../cpp/bstr-t-class.md), [`CComBSTR`](../atl/reference/ccombstr-class.md), [`CString`](../atl-mfc-shared/using-cstring.md), [`basic_string`](../standard-library/basic-string-class.md), and <xref:System.String?displayProperty=fullName>. In all cases, a copy of the string is made when converted to the new type. Any changes made to the new string will not affect the original string, and vice versa.
1212

1313
## Example: Convert from char *
1414

1515
### Description
1616

17-
This example demonstrates how to convert from a `char *` to the other string types listed above. A `char *` string (also known as a C style string) uses a null character to indicate the end of the string. C style strings usually require one byte per character, but can also use two bytes. In the examples below, `char *` strings are sometimes referred to as multibyte character strings because of the string data that results from converting from Unicode strings. Single byte and multibyte character (`MBCS`) functions can operate on `char *` strings.
17+
This example demonstrates how to convert from a `char *` to the string types listed above. A `char *` string (also known as a C-style string) uses a null character to indicate the end of the string. C-style strings usually require one byte per character, but can also use two bytes. In the examples below, `char *` strings are sometimes referred to as multibyte character strings because of the string data that results from converting from Unicode strings. Single byte and multibyte character (`MBCS`) functions can operate on `char *` strings.
1818

1919
### Code
2020

2121
```cpp
2222
// convert_from_char.cpp
23-
// compile with: /clr /link comsuppw.lib
23+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
2424

2525
#include <iostream>
2626
#include <stdlib.h>
@@ -35,7 +35,7 @@ using namespace System;
3535

3636
int main()
3737
{
38-
// Create and display a C style string, and then use it
38+
// Create and display a C-style string, and then use it
3939
// to create different kinds of strings.
4040
char *orig = "Hello, World!";
4141
cout << orig << " (char *)" << endl;
@@ -58,39 +58,39 @@ int main()
5858
// Display the result and indicate the type of string that it is.
5959
wcout << wcstring << _T(" (wchar_t *)") << endl;
6060

61-
// Convert the C style string to a _bstr_t string.
61+
// Convert the C-style string to a _bstr_t string.
6262
_bstr_t bstrt(orig);
6363
// Append the type of string to the new string
6464
// and then display the result.
6565
bstrt += " (_bstr_t)";
6666
cout << bstrt << endl;
6767

68-
// Convert the C style string to a CComBSTR string.
68+
// Convert the C-style string to a CComBSTR string.
6969
CComBSTR ccombstr(orig);
7070
if (ccombstr.Append(_T(" (CComBSTR)")) == S_OK)
7171
{
7272
CW2A printstr(ccombstr);
7373
cout << printstr << endl;
7474
}
7575

76-
// Convert the C style string to a CStringA and display it.
76+
// Convert the C-style string to a CStringA and display it.
7777
CStringA cstringa(orig);
7878
cstringa += " (CStringA)";
7979
cout << cstringa << endl;
8080

81-
// Convert the C style string to a CStringW and display it.
81+
// Convert the C-style string to a CStringW and display it.
8282
CStringW cstring(orig);
8383
cstring += " (CStringW)";
8484
// To display a CStringW correctly, use wcout and cast cstring
8585
// to (LPCTSTR).
8686
wcout << (LPCTSTR)cstring << endl;
8787

88-
// Convert the C style string to a basic_string and display it.
88+
// Convert the C-style string to a basic_string and display it.
8989
string basicstring(orig);
9090
basicstring += " (basic_string)";
9191
cout << basicstring << endl;
9292

93-
// Convert the C style string to a System::String and display it.
93+
// Convert the C-style string to a System::String and display it.
9494
String ^systemstring = gcnew String(orig);
9595
systemstring += " (System::String)";
9696
Console::WriteLine("{0}", systemstring);
@@ -109,17 +109,17 @@ Hello, World! (basic_string)
109109
Hello, World! (System::String)
110110
```
111111

112-
## Example: Convert from wchar_t *
112+
## Example: Convert from `wchar_t *`
113113

114114
### Description
115115

116-
This example demonstrates how to convert from a `wchar_t *` to the other string types listed above. Several string types, including `wchar_t *`, implement wide character formats. To convert a string between a multibyte and a wide character format, you can use a single function call like `mbstowcs_s` or a constructor invocation for a class like `CStringA`.
116+
This example demonstrates how to convert from a `wchar_t *` to other string types. Several string types, including `wchar_t *`, implement wide character formats. To convert a string between a multibyte and a wide character format, you can use a single function call like `mbstowcs_s` or a constructor invocation for a class like `CStringA`.
117117

118118
### Code
119119

120120
```cpp
121121
// convert_from_wchar_t.cpp
122-
// compile with: /clr /link comsuppw.lib
122+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
123123

124124
#include <iostream>
125125
#include <stdlib.h>
@@ -141,7 +141,7 @@ int main()
141141

142142
// Convert the wchar_t string to a char* string. Record
143143
// the length of the original string and add 1 to it to
144-
// account for the terminating null character.
144+
// account for the terminating NULL character.
145145
size_t origsize = wcslen(orig) + 1;
146146
size_t convertedChars = 0;
147147

@@ -231,17 +231,17 @@ Hello, World! (basic_string)
231231
Hello, World! (System::String)
232232
```
233233

234-
## Example: Convert from _bstr_t
234+
## Example: Convert from `_bstr_t`
235235

236236
### Description
237237

238-
This example demonstrates how to convert from a `_bstr_t` to the other string types listed above. The `_bstr_t` object is a way to encapsulate wide character `BSTR` strings. A BSTR string has a length value and does not use a null character to terminate the string, but the string type you convert to may require a terminating null.
238+
This example demonstrates how to convert from a `_bstr_t` to other string types. The `_bstr_t` object encapsulates wide character `BSTR` strings. A `BSTR` string has a length value and does not use a null character to terminate the string, but the string type you convert to may require a terminating `NULL`.
239239

240240
### Code
241241

242242
```cpp
243243
// convert_from_bstr_t.cpp
244-
// compile with: /clr /link comsuppw.lib
244+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
245245

246246
#include <iostream>
247247
#include <stdlib.h>
@@ -261,9 +261,9 @@ int main()
261261
_bstr_t orig("Hello, World!");
262262
wcout << orig << " (_bstr_t)" << endl;
263263

264-
// Convert the wide character _bstr_t string to a C style
264+
// Convert the wide character _bstr_t string to a C-style
265265
// string. To be safe, allocate two bytes for each character
266-
// in the char* string, including the terminating null.
266+
// in the char* string, including the terminating NULL.
267267
const size_t newsize = (orig.length()+1)*2;
268268
char *nstring = new char[newsize];
269269

@@ -329,17 +329,17 @@ Hello, World! (basic_string)
329329
Hello, World! (System::String)
330330
```
331331

332-
## Example: Convert from CComBSTR
332+
## Example: Convert from `CComBSTR`
333333

334334
### Description
335335

336-
This example demonstrates how to convert from a `CComBSTR` to the other string types listed above. Like _bstr_t, a `CComBSTR` object is a way to encapsulate wide character BSTR strings. A BSTR string has a length value and does not use a null character to terminate the string, but the string type you convert to may require a terminating null.
336+
This example demonstrates how to convert from a `CComBSTR` to other string types. Like `_bstr_t`, a `CComBSTR` object encapsulates wide character `BSTR` strings. A `BSTR` string has a length value and does not use a null character to terminate the string, but the string type you convert to may require a terminating `NULL`.
337337

338338
### Code
339339

340340
```cpp
341341
// convert_from_ccombstr.cpp
342-
// compile with: /clr /link comsuppw.lib
342+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
343343

344344
#include <iostream>
345345
#include <stdlib.h>
@@ -366,7 +366,7 @@ int main()
366366
// Convert a wide character CComBSTR string to a
367367
// regular multibyte char* string. Allocate enough space
368368
// in the new string for the largest possible result,
369-
// including space for a terminating null.
369+
// including space for a terminating NULL.
370370
const size_t newsize = (orig.Length()+1)*2;
371371
char *nstring = new char[newsize];
372372

@@ -437,19 +437,19 @@ Hello, World! (basic_string)
437437
Hello, World! (System::String)
438438
```
439439

440-
## Example: Convert from CString
440+
## Example: Convert from `CString`
441441

442442
### Description
443443

444-
This example demonstrates how to convert from a `CString` to the other string types listed above. `CString` is based on the TCHAR data type, which in turn depends on whether the symbol `_UNICODE` is defined. If `_UNICODE` is not defined, `TCHAR` is defined to be char and `CString` contains a multibyte character string; if `_UNICODE` is defined, `TCHAR` is defined to be **`wchar_t`** and `CString` contains a wide character string.
444+
This example demonstrates how to convert from a `CString` to other string types. `CString` is based on the TCHAR data type, which in turn depends on whether the symbol `_UNICODE` is defined. If `_UNICODE` is not defined, `TCHAR` is defined to be `char` and `CString` contains a multibyte character string; if `_UNICODE` is defined, `TCHAR` is defined to be **`wchar_t`** and `CString` contains a wide character string.
445445

446-
`CStringA` is the multibyte string always version of `CString`, `CStringW` is the wide character string only version. Neither `CStringA` nor `CStringW` use `_UNICODE` to determine how they should compile. `CStringA` and `CStringW` are used in this example to clarify minor differences in buffer size allocation and output handling.
446+
`CStringA` contains the `char` type and supports single-byte or multibyte strings. `CStringW` is the wide character version. Neither `CStringA` nor `CStringW` use `_UNICODE` to determine how they should compile. `CStringA` and `CStringW` are used in this example to clarify minor differences in buffer size allocation and output handling.
447447

448448
### Code
449449

450450
```cpp
451451
// convert_from_cstring.cpp
452-
// compile with: /clr /link comsuppw.lib
452+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
453453

454454
#include <iostream>
455455
#include <stdlib.h>
@@ -579,17 +579,17 @@ Hello, World! (basic_string)
579579
Hello, World! (System::String)
580580
```
581581

582-
## Example: Convert from basic_string
582+
## Example: Convert from `basic_string`
583583

584584
### Description
585585

586-
This example demonstrates how to convert from a `basic_string` to the other string types listed above.
586+
This example demonstrates how to convert from a `basic_string` to other string types.
587587

588588
### Code
589589

590590
```cpp
591591
// convert_from_basic_string.cpp
592-
// compile with: /clr /link comsuppw.lib
592+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
593593

594594
#include <iostream>
595595
#include <stdlib.h>
@@ -610,7 +610,7 @@ int main()
610610

611611
// Convert a wide character basic_string string to a multibyte char*
612612
// string. To be safe, we allocate two bytes for each character
613-
// in the original string, including the terminating null.
613+
// in the original string, including the terminating NULL.
614614
const size_t newsize = (strlen(orig.c_str()) + 1)*2;
615615
char *nstring = new char[newsize];
616616
strcpy_s(nstring, newsize, orig.c_str());
@@ -673,17 +673,17 @@ Hello, World! (CStringW)
673673
Hello, World! (System::String)
674674
```
675675

676-
## Example: Convert from System::String
676+
## Example: Convert from `System::String`
677677

678678
### Description
679679

680-
This example demonstrates how to convert from a wide character (Unicode) [System::String](/dotnet/api/system.string) to the other string types listed above.
680+
This example demonstrates how to convert from a wide character (Unicode) [System::String](/dotnet/api/system.string) to other string types.
681681

682682
### Code
683683

684684
```cpp
685685
// convert_from_system_string.cpp
686-
// compile with: /clr /link comsuppw.lib
686+
// compile with: /clr /Zc:twoPhase- /link comsuppw.lib
687687

688688
#include <iostream>
689689
#include <stdlib.h>
@@ -713,7 +713,7 @@ int main()
713713
// Make a copy of the System::String as a multibyte
714714
// char* string. Allocate two bytes in the multibyte
715715
// output string for every wide character in the input
716-
// string, including space for a terminating null.
716+
// string, including space for a terminating NULL.
717717
size_t origsize = wcslen(wch) + 1;
718718
const size_t newsize = origsize*2;
719719
size_t convertedChars = 0;
@@ -780,14 +780,14 @@ Hello, World! (basic_string)
780780

781781
## See also
782782

783-
[ATL and MFC String Conversion Macros](../atl/reference/string-conversion-macros.md)<br/>
784-
[CString Operations Relating to C-Style Strings](../atl-mfc-shared/cstring-operations-relating-to-c-style-strings.md)<br/>
785-
[How to: Convert Standard String to System::String](../dotnet/how-to-convert-standard-string-to-system-string.md)<br/>
786-
[How to: Convert System::String to Standard String](../dotnet/how-to-convert-system-string-to-standard-string.md)<br/>
787-
[How to: Convert System::String to wchar_t* or char\*](../dotnet/how-to-convert-system-string-to-wchar-t-star-or-char-star.md)<br/>
788-
[Programming with CComBSTR](../atl/programming-with-ccombstr-atl.md)<br/>
789-
[mbstowcs_s, _mbstowcs_s_l](../c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l.md)<br/>
790-
[wcstombs_s, _wcstombs_s_l](../c-runtime-library/reference/wcstombs-s-wcstombs-s-l.md)<br/>
791-
[strcpy_s, wcscpy_s, _mbscpy_s](../c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s.md)<br/>
792-
[strcat_s, wcscat_s, _mbscat_s](../c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s.md)<br/>
793-
[pin_ptr (C++/CLI)](../extensions/pin-ptr-cpp-cli.md)
783+
[ATL and MFC String Conversion Macros](../atl/reference/string-conversion-macros.md)\
784+
[CString Operations Relating to C-Style Strings](../atl-mfc-shared/cstring-operations-relating-to-c-style-strings.md)\
785+
[How to: Convert Standard String to `System::String`](../dotnet/how-to-convert-standard-string-to-system-string.md)\
786+
[How to: Convert `System::String` to Standard String](../dotnet/how-to-convert-system-string-to-standard-string.md)\
787+
[How to: Convert `System::String` to `wchar_t*` or `char*`](../dotnet/how-to-convert-system-string-to-wchar-t-star-or-char-star.md)\
788+
[Programming with `CComBSTR`](../atl/programming-with-ccombstr-atl.md)\
789+
[`mbstowcs_s, _mbstowcs_s_l`](../c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l.md)\
790+
[`wcstombs_s, _wcstombs_s_l`](../c-runtime-library/reference/wcstombs-s-wcstombs-s-l.md)\
791+
[`strcpy_s, wcscpy_s, _mbscpy_s`](../c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s.md)\
792+
[`strcat_s, wcscat_s, _mbscat_s`](../c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s.md)\
793+
[`pin_ptr` (C++/CLI)](../extensions/pin-ptr-cpp-cli.md)

0 commit comments

Comments
 (0)