VS中各种类型的转换.docx
- 文档编号:4598500
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:22
- 大小:19.36KB
VS中各种类型的转换.docx
《VS中各种类型的转换.docx》由会员分享,可在线阅读,更多相关《VS中各种类型的转换.docx(22页珍藏版)》请在冰豆网上搜索。
VS中各种类型的转换
VS2010中各种类型的转换
此示例演示如何从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,nstring,newsize,orig,_TRUNCATE); //appendthetypeofstringtothenewstring. _mbscat_s((unsignedchar*)nstring,newsize+strConcatsize,(unsignedchar*)strConcat); //Displaytheresult. cout< //Convertawchar_ttoa_bstr_tstringanddisplayit. _bstr_tbstrt(orig); bstrt+="(_bstr_t)"; cout< //Convertthewchar_tstringtoaBSTRwidecharacterstring //byusingtheATLCComBSTRwrapperclassforBSTRstrings. //Thendisplaytheresult. CComBSTRccombstr(orig); if(ccombstr.Append(_T("(CComBSTR)"))==S_OK) { //CW2Aconvertsthestringinccombstrtoamultibyte //stringinprintstr,usedherefordisplayoutput. CW2Aprintstr(ccombstr); cout< //Thefollowinglineofcodeisaneasierwayto //displaywidecharacterstrings: //wcout<<(LPCTSTR)ccombstr< } //Convertawidewchar_tstringtoamultibyteCStringA, //appendthetypeofstringtoit,anddisplaytheresult. CStringAcstringa(orig); cstringa+="(CStringA)"; cout< //Convertawidecharacterwchar_tstringtoawide //characterCStringWstringandappendthetypeofstringtoit CStringWcstring(orig); cstring+="(CStringW)"; //TodisplayaCStringWcorrectly,usewcoutandcastcstring //to(LPCTSTR). wcout<<(LPCTSTR)cstring< //Convertthewidecharacterwchar_tstringtoa //basic_string,appendthetypeofstringtoit,and //displaytheresult. wstringbasicstring(orig); basicstring+=_T("(basic_string)"); wcout< //Convertawidecharacterwchar_tstringtoa //System: : Stringstring,appendthetypeofstringtoit, //anddisplaytheresult. String^systemstring=gcnewString(orig); systemstring+="(System: : String)"; Console: : WriteLine("{0}",systemstring); deletesystemstring; } 输出 复制 Hello,World! (wchar_t*) Hello,World! (char*) Hello,World! (_bstr_t) Hello,World! (CComBSTR) Hello,World! (CStringA) Hello,World! (CStringW) Hello,World! (basic_string) Hello,World! (System: : String) 示例 -------------------------------------------------------------------------------- 说明 此示例演示如何从_bstr_t转换为上面列出的其他字符串类型。 _bstr_t对象是一种封装宽字符BSTR字符串的方法。 BSTR字符串具有长度值且不使用null字符终止字符串,但所要转换到的目标字符串类型可能需要终止null。 代码 复制 //convert_from_bstr_t.cpp //compilewith: /clr/linkcomsuppw.lib #include #include #include #include"atlbase.h" #include"atlstr.h" #include"comutil.h" usingnamespacestd; usingnamespaceSystem; intmain() { //Createa_bstr_tstring,displaytheresult,andindicatethe //typeofstringthatitis. _bstr_torig("Hello,World! "); wcout< //Convertthewidecharacter_bstr_tstringtoaCstyle //string.Tobesafe,allocatetwobytesforeachcharacter //inthechar*string,includingtheterminatingnull. constsize_tnewsize=(orig.length()+1)*2; char*nstring=newchar[newsize]; //Usesthe_bstr_toperator(char*)toobtainanull //terminatedstringfromthe_bstr_tobjectfor //nstring. strcpy_s(nstring,newsize,(char*)orig); strcat_s(nstring,newsize,"(char*)"); cout< //Preparethetypeofstringtoappendtotheresult. wchar_tstrConcat[]=_T("(wchar_t*)"); size_tstrConcatLen=wcslen(strConcat)+1; //Converta_bstr_ttoawchar_t*string. constsize_twidesize=orig.length()+strConcatLen; wchar_t*wcstring=newwchar_t[newsize]; wcscpy_s(wcstring,widesize,(wchar_t*)orig); wcscat_s(wcstring,widesize,strConcat); wcout< //Converta_bstr_tstringtoaCComBSTRstring. CComBSTRccombstr((char*)orig); if(ccombstr.Append(_T("(CComBSTR)"))==S_OK) { CW2Aprintstr(ccombstr); cout< } //Converta_bstr_ttoaCStringAstring. CStringAcstringa(orig.GetBSTR()); cstringa+="(CStringA)"; cout< //Converta_bstr_ttoaCStringWstring. CStringWcstring(orig.GetBSTR()); cstring+="(CStringW)"; //Todisplayacstringcorrectly,usewcoutand //"cast"thecstringto(LPCTSTR). wcout<<(LPCTSTR)cstring< //Convertthe_bstr_ttoabasic_string. stringbasicstring((char*)orig); basicstring+="(basic_string)"; cout< //Convertthe_bstr_ttoaSystem: : String. String^systemstring=gcnewString((char*)orig); systemstring+="(System: : String)"; Console: : WriteLine("{0}",systemstring); deletesystemstring; } 输出 复制 Hello,World! (_bstr_t) Hello,World! (char*) Hello,World! (wchar_t*) Hello,World! (CComBSTR) Hello,World! (CStringA) Hello,World! (CStringW) Hello,World! (basic_string) Hello,World! (System: : String) 示例 -------------------------------------------------------------------------------- 说明 此示例演示如何从CComBSTR转换为上面列出的其他字符串类型。 与_bstr_t一样,CComBSTR对象是一种封装宽字符BSTR字符串的方法。 BSTR字符串具有长度值且不使用null字符终止字符串,但所要转换到的目标字符串类型可能需要终止null。 代码 复制 //convert_from_ccombstr.cpp //compilewith: /clr/linkcomsuppw.lib #include #include #include #include"atlbase.h" #include"atlstr.h" #include"comutil.h" #include"vcclr.h" usingname
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VS 各种 类型 转换