如何在各种字符串类型之间进行转换 VS.docx

上传人:b****1 文档编号:2477310 上传时间:2022-10-30 格式:DOCX 页数:21 大小:19.63KB
下载 相关 举报
如何在各种字符串类型之间进行转换 VS.docx_第1页
第1页 / 共21页
如何在各种字符串类型之间进行转换 VS.docx_第2页
第2页 / 共21页
如何在各种字符串类型之间进行转换 VS.docx_第3页
第3页 / 共21页
如何在各种字符串类型之间进行转换 VS.docx_第4页
第4页 / 共21页
如何在各种字符串类型之间进行转换 VS.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

如何在各种字符串类型之间进行转换 VS.docx

《如何在各种字符串类型之间进行转换 VS.docx》由会员分享,可在线阅读,更多相关《如何在各种字符串类型之间进行转换 VS.docx(21页珍藏版)》请在冰豆网上搜索。

如何在各种字符串类型之间进行转换 VS.docx

如何在各种字符串类型之间进行转换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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 职业规划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1