学生联盟网为您提供优质参考范文! 体会工作报告法律咨询精彩演讲各类材料
当前位置: 学生联盟网 > 高等教育 > 管理学 > C关于数据类型转换大全

C关于数据类型转换大全

时间:2021-11-09 13:14:47 来源:学生联盟网

C有关数据类型转换大全来源中国自学编程网发布日期2007-05-21int i 100;long l 2001;float f300.2;double d12345.119;char username程佩君 ;char temp200;char *buf;CStri ng str;_varia nt_t v1;_bstr_t v2;一、其它数据类型转换为字符串短整型intitoai,temp,10;/将i转换为字符串放入temp中,最后一个数字表示十进制itoai,temp,2; ///按二进制方式转换长整型longltoal,temp,10;浮点数float,double用fcvt可以完成转换,这是MSDN中的例子int decimal,sig n;char *buffer;double source 3.1415926535;buffer _fcvt source,7,运行结果source 3.1415926535 buffer 31415927 decimal 1 sign 0decimal表示小数点的位置,sign表示符号0为正数,1为负数CString 变量str 2008 北京奥运 ;buf LPSTRLPCTSTRstr;BSTR变量BSTR bstrValue SysAllocStringL程序员 ;char * buf _com_utilCo nvertBSTRToStri ngbstrValue;SysFreeStri ngbstrValue;AfxMessageBoxbuf;deletebuf;CComBSTR 变量CComBSTR bstrVartest;char *buf _com_utilCo nvertBSTRToStri ngbstrVar.m_str;AfxMessageBoxbuf;deletebuf;_bstr_t 变量_bstr_t类型是对BSTR的封装,因为已经重载了操作符,所以很容易使用_bstr_t bstrVar(test);con st char *buf bstrVar;/// 不要修改 buf 中的内容AfxMessageBoxbuf;通用方法(针对非COM数据类型)用sprintf完成转换char buffer200; char c 1int; i 35; long j 1000; float f 1.7320534f; sprintf buffer,c,c; spri ntf buffer,d,i; spri ntf buffer,d,j; spri ntf buffer,f,f;二、字符串转换为其它数据类型strcpytemp,123;短整型inti atoitemp;长整型longl atoltemp;浮点doubled atoftemp;CString 变量CStri ng n ame temp;BSTR变量BSTR bstrValue SysAllocStringL程序员 ;...///完成对bstrValue的使用SysFreeStri ngbstrValue;CComBSTR 变量CComBSTR类型变量可以直接赋值CComBSTR bstrVar1test;CComBSTR bstrVar2temp;_bstr_t 变量Page_bstr_t类型的变量可以直接赋值_bstr_t bstrVar1test;_bstr_t bstrVar2temp;三、其它数据类型转换到CStri ng使用CString的成员函数at来转换,例如 整数(int)str.atd,i;浮点数floatstr.atf,i;字符串指针char *等已经被CString构造函数支持的数据类型可以直接赋值str user name;对于at所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给 CString变量。四、BSTR、_bstr_t 与 CComBSTRCComBSTR 是ATL对BSTR的封装,_bstr_t是C对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。char *转换到BSTR可以这样BSTR b_com_utilConvertStringToBSTR 数据 ;/// 使用前需要加上 comutil.h 和 comsupp .libSysFreeStri ngbstrValue;反之可以使用char *p_com_utilCo nvertBSTRToStri ngb;delete p;具体可以参考一,二段落里的具体说明。CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行,,等操作,所以使用非常方便。特别是_bstr_t,建议大家使用它。五、VARIANT 、_variant_t 与 COleVariantVARIANT 的结构可以参考头文件 VC98WIncludeWOAIDL.H中关于结构体tagVARIANT 的定义。对于VARIANT变量的赋值首先给 vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子VARIANT va;int a2001;va.vtVT_l4;/指明整型数据va.lVala; /// 赋值_variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理 这些数据类型。使用时需加上include comdef.h例如long 1222;ing i100;_varia nt_t lVall;IVal Ion gi;COIeVariant的使用与_variant_t的方法基本一样,请参考如下例子COIeVariant v3 字符串 ,v4 Iong1999;CStri ng str BSTRv3.pbstrVal;long i v4.IVaI;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////本主题演示如何将各种C字符串类型转换为其他字符串。可以转换的字符串类型包括char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情 况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都 不会影响原始字符串,反之亦然。从char *转换示例说明 此示例演示如何从char *转换为上面列出的其他字符串类型。// con vert_from_char.cpp// compiIe with /cIr /Ii nk comsuppw.Iib in cIude iostreami nclude stdlib.h Pagein clude stri ngin clude atlbase.hi nclude atlstr.hin clude comutil.h using n amespace std;using n amespace System;int mai nchar *orig Hello,World;cout orig char * en dl;// Con vert to a wchar_t*size_t origsize strle n orig 1;const size_t n ewsize 100;size_t con vertedChars 0;wchar_t wcstri ngn ewsize;mbstowcs_s wcscat_swcstri ng,L wchar_t *;wcout wcstri ng en dl;// Con vert to a _bstr_t_bstr_t bstrtorig;bstrt _bstr_t;cout bstrt en dl;// Co nvert to a CComBSTRCComBSTR ccombstrorig;if ccombstr.Appe ndL CComBSTR S_OKCW2A prin tstrccombstr;cout prin tstr en dl;// Convert to a CStri ngCStri ng cstri ngorig;cstring CStri ng;cout cstri ng en dl;// Con vert to a basic_stri ngstri ng basicstri ngorig;basicstri ng basic_stri ng;cout basicstri ng en dl;// Con vert to a SystemStri ngString Asystemstri ng gcnew Strin gorig;systemstri ng SystemStri ng;Con soleWriteLi ne0,systemstri ng; Page delete systemstri ng;输出Hello,World char *Hello,World wchar_t *Hello,World _bstr_tHello,World CComBSTRHello,World CStri ngHello,World basic_stri ngHello,World SystemStri ng从wchar_t *转换示例说明此示例演示如何从wchar_t *转换为上面列出的其他字符串类型。// con vert_from_wchar_t.cpp// compile with /clr /li nk comsuppw.libin elude iostreami nclude stdlib.hin elude stri ngin elude atlbase.hi nclude atlstr.hi nclude comutil.husing n amespace std;using n amespace System;int mai nwchar_t *orig LHello,World;wcout orig L wchar_t * en dl;// Con vert to a char*size_t origsize wcsle n orig 1;const size_t n ewsize 100;size_t con vertedChars 0;char n stri ngn ewsize;wcstombs_s strcat_s nstri ng,char *;cout n stri ng en dl;// Con vert to a _bstr_t_bstr_t bstrtorig;bstrt _bstr_t;cout bstrt en dl;// Convert to a CComBSTRCComBSTR ccombstrorig;if ccombstr.Appe ndL CComBSTR S_OKCW2A prin tstrccombstr;cout prin tstr en dl;// Convert to a CStri ngCStri ng cstri ngorig;cstring CStri ng;cout cstri ng en dl; Page// Con vert to a basic_stri ngwstri ng basicstri ngorig;basicstri ng L basic_stri ng;wcout basicstri ng en dl;// Con vert to a SystemStri ngString Asystemstri ng gcnew Strin gorig;systemstri ng SystemStri ng;Con soleWriteLi ne0,systemstri ng;delete systemstri ng;输出Hello,World wchar_t *Hello,World char *Hello,World _bstr_tHello,World CComBSTRHello,World CStri ngHello,World basic_stri ngHello,World SystemStri ng从_bstr_t转换示例说明此示例演示如何从_bstr_t转换为上面列出的其他字符串类型。// con vert_from_bstr_t.cpp// compile with /clr /li nk comsuppw.libin clude iostreami nclude stdlib.hin clude stri ngin clude atlbase.hi nclude atlstr.hi nclude comutil.husing n amespace std;using n amespace System;int mai n_bstr_t origHello,World;wcout orig _bstr_t en dl;// Con vert to a char*const size_t n ewsize 100;char n stri ngn ewsize;strcpy_s n stri ng,char *orig; strcat_s nstri ng,char *;cout n stri ng en dl;// Con vert to a wchar_t*wchar_t wcstri ngn ewsize;wcscpy_swcstri ng,wchar_t *orig;wcscat_swcstri ng,L wchar_t *;wcout wcstri ng en dl;// Co nvert to a CComBSTRCComBSTR ccombstrchar *orig;if ccombstr.Appe ndL CComBSTR S_OK Page CW2A prin tstrccombstr; cout prin tstr en dl;// Convert to a CStri ngCStri ng cstri ngchar *orig;cstring CStri ng;cout cstri ng en dl;// Con vert to a basic_stri ngstri ng basicstri ngchar *orig;basicstri ng basic_stri ng;cout basicstri ng en dl;// Con vert to a SystemStri ngString Asystemstri ng gcnew Strin gchar *orig; systemstri ng SystemStri ng;Con soleWriteLi ne0,systemstri ng;delete systemstri ng;输出Hello,World _bstr_tHello,World char *Hello,World wchar_t *Hello,World CComBSTRHello,World CStri ngHello,World basic_stri ngHello,World SystemStri ng从CComBSTR转换示例说明此示例演示如何从CComBSTR转换为上面列出的其他字符串类型。// con vert_from_ccombstr.cpp// compile with /clr /li nk comsuppw.libin clude iostreami nclude stdlib.hin clude stri ngin clude atlbase.hi nclude atlstr.hi nclude comutil.hi nclude vcclr.husing n amespace std;using n amespace System;using n amespace SystemR un timel nteropServices;int mai nCComBSTR origHello,World;CW2A prin tstrorig;cout prin tstr CComBSTR endl;// Con vert to a char*const size_t n ewsize 100;char n stri ngn ewsize;CW2A tmpstrlorig;strcpy_s n stri ng,tmpstrl; Pagestrcat_s nstri ng,char *;cout n stri ng en dl;// Con vert to a wchar_t*wchar_t wcstri ngn ewsize;wcscpy_swcstri ng,orig;wcscat_swcstri ng,L wchar_t *;wcout wcstri ng en dl;// Con vert to a _bstr_t_bstr_t bstrtorig;bstrt _bstr_t;cout bstrt en dl;// Convert to a CStri ngCStri ng cstri ngorig; cstring CStri ng; cout cstri ng en dl;// Con vert to a basic_stri ngwstri ng basicstri ngorig;basicstri ng L basic_stri ng;wcout basicstri ng en dl;// Con vert to a SystemStri ngString Asystemstri ng gcnew Strin gorig;systemstri ng SystemStri ng;Con soleWriteLi ne0,systemstri ng; delete systemstri ng;输出Hello,World CComBSTRHello,World char *Hello,World wchar_t *Hello,World _bstr_tHello,World CStri ngHello,World basic_stri ngHello,World SystemStri ng从CString转换示例说明此示例演示如何从CString转换为上面列出的其他字符串类型。// con vert_from_cstri ng.cpp// compile with /clr /li nk comsuppw.libin clude iostreami nclude stdlib.hin clude stri ngin clude atlbase.hi nclude atlstr.hi nclude comutil.husing n amespace std;using n amespace System;int mai nCStri ng origHello,World;wcout orig CStri ng endl; Page// Con vert to a char*const size_t n ewsize 100;char n stri ngn ewsize;strcpy_s n stri ng,orig;strcat_s nstri ng,char *;cout n stri ng en dl;// Con vert to a wchar_t*// You must first convert to a char * for this to work.size_t origsize strle n orig 1;size_t con vertedChars 0;wchar_t wcstri ngn ewsize;mbstowcs_s wcscat_swcstri ng,L wchar_t *;wcout wcstri ng en dl;// Convert to a _bstr_t_bstr_t bstrtorig;bstrt _bstr_t;cout bstrt en dl;// Co nvert to a CComBSTRCComBSTR ccombstrorig;if ccombstr.Appe ndL CComBSTR S_OK CW2A prin tstrccombstr;cout prin tstr en dl;// Con vert to a basic_stri ngstri ng basicstri ngorig;basicstri ng basic_stri ng;cout basicstri ng en dl;// Con vert to a SystemStri ngString Asystemstri ng gcnew Strin gorig; systemstri ng SystemStri ng;Con soleWriteLi ne0,systemstri ng;delete systemstri ng;输出Hello,World CStri ngHello,World char * PageHello,World wchar_t *Hello,World _bstr_tHello,World CComBSTRHello,World basic_stri ngHello,World SystemStri ng从 basic_string 转换示例说明此示例演示如何从basic_stri ng转换为上面列出的其他字符串类型。// con vert_from_basic_stri ng.cpp// compile with /clr /li nk comsuppw.libin clude iostreami nclude stdlib.hin clude stri ngin clude atlbase.hi nclude atlstr.hi nclude comutil.husing n amespace std;using n amespace System;int mai nstri ng origHello,World;cout orig basic_stri ng en dl;// Con vert to a char*const size_t n ewsize 100;char n stri ngn ewsize;strcpy_s n stri ng,orig.c_str;strcat_s nstri ng,char *;cout n stri ng en dl;// Con vert to a wchar_t*// You must first convert to a char * for this to work.size_t origsize strle n orig.c_str 1;size_t con vertedChars 0;wchar_t wcstri ngn ewsize;mbstowcs_swcscat_swcstri ng,L wchar_t *;wcout wcstri ng en dl;// Con vert to a _bstr_t_bstr_t bstrtorig.c_str;bstrt _bstr_t;cout bstrt en dl;// Co nvert to a CComBSTRCComBSTR ccombstrorig.c_str;if ccombstr.Appe ndL CComBSTR S_OKCW2A prin tstrccombstr; Pagecout prin tstr en dl;// Convert to a CStri ngCStri ng cstri ngorig.c_str;cstring CStri ng;cout cstri ng en dl;// Con vert to a SystemStri ngString Asystemstri ng gcnew Strin gorig.c_str;systemstri ng SystemStri ng;Con soleWriteLi ne0,systemstri ng;delete systemstri ng;输出Hello,World basic_stri ngHello,World char *Hello,World wchar_t *Hello,World _bstr_tHello,World CComBSTRHello,World CStri ngHello,World SystemStri ng从 SystemString 转换示例说明此示例演示如何从System.String转换为上面列出的其他字符串类型。// con vert_from_system_stri ng.cpp// compile with /clr /li nk comsuppw.lib in clude iostream i nclude stdlib.hin elude stri ngin elude atlbase.hi nclude atlstr.hi nclude comutil.hi nclude vcclr.husing n amespace std;using n amespace System;using n amespace SystemR un timel nteropServices;int mai nStri ng Aorig gcnew Strin gHello,World;Con soleWriteLi ne0 SystemStri ng,orig;pin _ptrc onst wchar_t wch PtrToStri ngCharsorig;// Con vert to a char*size_t origsize wcsle n wch 1;const size_t n ewsize 100;size_t con vertedChars 0;char n stri ngn ewsize;wcstombs_s strcat_s nstri ng,char *;cout n stri ng en dl;// Con vert to a wchar_t* Pagewchar_t wcstri ngn ewsize;wcscpy_swcstri ng,wch;wcscat_swcstri ng,L wchar_t *;wcout wcstri ng en dl;// Con vert to a _bstr_t_bstr_t bstrtwch;bstrt _bstr_t;cout bstrt en dl;// Co nvert to a CComBSTRCComBSTR ccombstrwch;if ccombstr.Appe ndL CComBSTR S_OKCW2A prin tstrccombstr;cout prin tstr en dl;// Convert to a CStri ng CStri ng cstri ngwch; cstring CStri ng; cout cstri ng en dl;// Con vert to a basic_stri ng wstri ng basicstri ngwch; basicstri ng L basic_stri ng; wcout basicstri ng en dl;delete orig;输出Hello,World SystemStri ngHello,World char *Hello,World wchar_t *Hello,World _bstr_tHello,World CComBSTRHello,World CStri ngHello,World basic_stri ng