如何在各种字符串类型之间进行转换 VS.docx
《如何在各种字符串类型之间进行转换 VS.docx》由会员分享,可在线阅读,更多相关《如何在各种字符串类型之间进行转换 VS.docx(21页珍藏版)》请在冰豆网上搜索。
![如何在各种字符串类型之间进行转换 VS.docx](https://file1.bdocx.com/fileroot1/2022-10/29/b95f24eb-0013-4185-8e3b-280f346f2b73/b95f24eb-0013-4185-8e3b-280f346f2b731.gif)
如何在各种字符串类型之间进行转换VS
如何:
在各种字符串类型之间进行转换
VisualStudio2010其他版本VisualStudio2008VisualStudio2005
本主题演示如何将各种VisualC++字符串类型转换为其他字符串。
可以转换的字符串类型包括char*、wchar_t*、_bstr_t、CComBSTR、CString、basic_string和System.String。
在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。
对新字符串进行的任何更改都不会影响原始字符串,反之亦然。
示例
--------------------------------------------------------------------------------
说明
此示例演示如何从char*转换为上面列出的其他字符串类型。
char*字符串(也称为C样式字符串)使用null字符指示字符串的末尾。
C样式字符串通常每个字符需要一个字节,但也可以使用两个字节。
在下面的示例中,char*字符串有时称为多字节字符字符串,因为该字符串数据是从Unicode字符串转换得到的。
可对char*字符串执行单字节和多字节字符(MBCS)函数运算。
代码
复制//convert_from_char.cpp
//compilewith:
/clr/linkcomsuppw.lib
#include
#include
#include
#include"atlbase.h"
#include"atlstr.h"
#include"comutil.h"
usingnamespacestd;
usingnamespaceSystem;
intmain()
{
//CreateanddisplayaCstylestring,andthenuseit
//tocreatedifferentkindsofstrings.
char*orig="Hello,World!
";
cout<//newsizedescribesthelengthofthe
//wchar_tstringcalledwcstringintermsofthenumber
//ofwidecharacters,notthenumberofbytes.
size_tnewsize=strlen(orig)+1;
//Thefollowingcreatesabufferlargeenoughtocontain
//theexactnumberofcharactersintheoriginalstring
//inthenewformat.Ifyouwanttoaddmorecharacters
//totheendofthestring,increasethevalueofnewsize
//toincreasethesizeofthebuffer.
wchar_t*wcstring=newwchar_t[newsize];
//Convertchar*stringtoawchar_t*string.
size_tconvertedChars=0;
mbstowcs_s(&convertedChars,wcstring,newsize,orig,_TRUNCATE);
//Displaytheresultandindicatethetypeofstringthatitis.
wcout<//ConverttheCstylestringtoa_bstr_tstring.
_bstr_tbstrt(orig);
//Appendthetypeofstringtothenewstring
//andthendisplaytheresult.
bstrt+="(_bstr_t)";
cout<//ConverttheCstylestringtoaCComBSTRstring.
CComBSTRccombstr(orig);
if(ccombstr.Append(_T("(CComBSTR)"))==S_OK)
{
CW2Aprintstr(ccombstr);
cout<}
//ConverttheCstylestringtoaCstringAanddisplayit.
CStringAcstringa(orig);
cstringa+="(CStringA)";
cout<//ConverttheCstylestringtoaCStringWanddisplayit.
CStringWcstring(orig);
cstring+="(CStringW)";
//TodisplayaCStringWcorrectly,usewcoutandcastcstring
//to(LPCTSTR).
wcout<<(LPCTSTR)cstring<//ConverttheCstylestringtoabasic_stringanddisplayit.
stringbasicstring(orig);
basicstring+="(basic_string)";
cout<//ConverttheCstylestringtoaSystem:
:
Stringanddisplayit.
String^systemstring=gcnewString(orig);
systemstring+="(System:
:
String)";
Console:
:
WriteLine("{0}",systemstring);
deletesystemstring;
}
输出
复制Hello,World!
(char*)
Hello,World!
(wchar_t*)
Hello,World!
(_bstr_t)
Hello,World!
(CComBSTR)
Hello,World!
(CStringA)
Hello,World!
(CStringW)
Hello,World!
(basic_string)
Hello,World!
(System:
:
String)
示例
--------------------------------------------------------------------------------
说明
此示例演示如何从wchar_t*转换为上面列出的其他字符串类型。
包括wchar_t*在内的一些字符串类型可实现宽字符格式。
若要在多字节和宽字符格式之间转换字符串,则可以使用单个函数调用(如mbstowcs_s),也可以使用针对类的构造函数调用(如CStringA)。
代码
复制//convert_from_wchar_t.cpp
//compilewith:
/clr/linkcomsuppw.lib
#include
#include
#include
#include"atlbase.h"
#include"atlstr.h"
#include"comutil.h"
usingnamespacestd;
usingnamespaceSystem;
intmain()
{
//Createastringofwidecharacters,displayit,andthen
//usethisstringtocreateothertypesofstrings.
wchar_t*orig=_T("Hello,World!
");
wcout<//Convertthewchar_tstringtoachar*string.Record
//.thelengthoftheoriginalstringandadd1toitto
//.accountfortheterminatingnullcharacter.
size_torigsize=wcslen(orig)+1;
size_tconvertedChars=0;
//Useamultibytestringtoappendthetypeofstring
//tothenewstringbeforedisplayingtheresult.
charstrConcat[]="(char*)";
size_tstrConcatsize=(strlen(strConcat)+1)*2;
//Allocatetwobytesinthemultibyteoutputstringforeverywide
//characterintheinputstring(includingawidecharacter
//null).Becauseamultibytecharactercanbeoneortwobytes,
//youshouldallottwobytesforeachcharacter.Havingextra
//spaceforthenewstringisnotanerror,buthaving
//insufficientspaceisapotentialsecurityproblem.
constsize_tnewsize=origsize*2;
//Thenewstringwillcontainaconvertedcopyoftheoriginal
//stringplusthetypeofstringappendedtoit.
char*nstring=newchar[newsize+strConcatsize];
//Putacopyoftheconvertedstringintonstring
wcstombs_s(&convertedChars,nstri