C有关数据类型转换大全_第1页
C有关数据类型转换大全_第2页
C有关数据类型转换大全_第3页
C有关数据类型转换大全_第4页
C有关数据类型转换大全_第5页
免费预览已结束,剩余10页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+有关数据类型转换大全 来源:中国自学编程网 发布日期:2007-05-21 int i = 100; long l = 2001; float f=300.2; double d=12345.119; char username="程佩君" char temp200; char *buf; CString str; _variant_t v1; _bstr_t v2; 一、其它数据类型转换为字符串 短整型(int) itoa(i,temp,10);/将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); /按二进制方式转换 长整型(lon

2、g) ltoa(l,temp,10); 浮点数(float,double) 用fcvt可以完成转换,这是MSDN中的例子: int decimal, sign; char *buffer; double source = 3.1415926535; buffer = _fcvt( source, 7, &decimal, &sign ); 运行结果:source: 3.1415926535 buffer: 31415927 decimal: 1 sign: 0 decimal表示小数点的位置,sign表示符号:0为正数,1为负数 CString变量 str = "20

3、08北京奥运" buf = (LPSTR)(LPCTSTR)str; BSTR变量 BSTR bstrValue = :SysAllocString(L"程序员"); char * buf = _com_util:ConvertBSTRToString(bstrValue); SysFreeString(bstrValue); AfxMessageBox(buf); delete(buf); CComBSTR变量 CComBSTR bstrVar("test"); char *buf = _com_util:ConvertBSTRToStrin

4、g(bstrVar.m_str); AfxMessageBox(buf); delete(buf); _bstr_t变量 _bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用 _bstr_t bstrVar("test"); const char *buf = bstrVar;/不要修改buf中的内容 AfxMessageBox(buf); 通用方法(针对非COM数据类型) 用sprintf完成转换 char buffer200; char c = 1; int i = 35; long j = 1000; float f = 1.7320534f;

5、 sprintf( buffer, "%c",c); sprintf( buffer, "%d",i); sprintf( buffer, "%d",j); sprintf( buffer, "%f",f); 二、字符串转换为其它数据类型 strcpy(temp,"123"); 短整型(int) i = atoi(temp); 长整型(long) l = atol(temp); 浮点(double) d = atof(temp); CString变量 CString name = temp; B

6、STR变量 BSTR bstrValue = :SysAllocString(L"程序员"); ./完成对bstrValue的使用 SysFreeString(bstrValue); CComBSTR变量 CComBSTR类型变量可以直接赋值 CComBSTR bstrVar1("test"); CComBSTR bstrVar2(temp); _bstr_t变量 Page_bstr_t类型的变量可以直接赋值 _bstr_t bstrVar1("test"); _bstr_t bstrVar2(temp); 三、其它数据类型转换到CS

7、tring 使用CString的成员函数Format来转换,例如: 整数(int) str.Format("%d",i); 浮点数(float) str.Format("%f",i); 字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值 str = username; 对于Format所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给CString变量。 四、BSTR、_bstr_t与CComBSTR CComBSTR 是ATL对BSTR的封装,_bstr_t是C+

8、对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。 char *转换到BSTR可以这样: BSTR b=_com_util:ConvertStringToBSTR("数据");/使用前需要加上comutil.h和comsupp.lib SysFreeString(bstrValue); 反之可以使用 char *p=_com_util:ConvertBSTRToString(b); delete p; 具体可以参考一,二段落里的具体说明。 CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行=,!=,=等操作,所以使用非常方便。 特别是_

9、bstr_t,建议大家使用它。 五、VARIANT 、_variant_t 与 COleVariant VARIANT的结构可以参考头文件VC98IncludeOAIDL.H中关于结构体tagVARIANT的定义。 对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子: VARIANT va; int a=2001; va.vt=VT_I4;/指明整型数据 va.lVal=a; /赋值 _variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。 使用时需加上#include <co

10、mdef.h> 例如: long l=222; ing i=100; _variant_t lVal(l); lVal = (long)i; COleVariant的使用与_variant_t的方法基本一样,请参考如下例子: COleVariant v3 = "字符串", v4 = (long)1999; CString str =(BSTR)v3.pbstrVal; long i = v4.lVal; / 本主题演示如何将各种 C+ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString

11、、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。 从 char * 转换 示例 说明 此示例演示如何从 char * 转换为上面列出的其他字符串类型。 / convert_from_char.cpp / compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> Page#include <string> #include "a

12、tlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() char *orig = "Hello, World!" cout << orig << " (char *)" << endl; / Convert to a wchar_t* size_t origsize = strlen(orig) + 1; cons

13、t size_t newsize = 100; size_t convertedChars = 0; wchar_t wcstringnewsize; mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; / Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t

14、)" cout << bstrt << endl; / Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") = S_OK) CW2A printstr(ccombstr); cout << printstr << endl; / Convert to a CString CString cstring(orig); cstring += " (CString)" cout <&l

15、t; cstring << endl; / Convert to a basic_string string basicstring(orig); basicstring += " (basic_string)" cout << basicstring << endl; / Convert to a System:String String systemstring = gcnew String(orig); systemstring += " (System:String)" Console:WriteLine(&q

16、uot;0", systemstring); Page delete systemstring; 输出 Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System:String) 从 wchar_t * 转换 示例 说明 此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。 / con

17、vert_from_wchar_t.cpp / compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() wchar_t *orig =

18、 L"Hello, World!" wcout << orig << L" (wchar_t *)" << endl; / Convert to a char* size_t origsize = wcslen(orig) + 1; const size_t newsize = 100; size_t convertedChars = 0; char nstringnewsize; wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);

19、strcat_s(nstring, " (char *)"); cout << nstring << endl; / Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)" cout << bstrt << endl; / Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.Append(L" (CComBSTR)") = S_OK) CW2A pr

20、intstr(ccombstr); cout << printstr << endl; / Convert to a CString CString cstring(orig); cstring += " (CString)" cout << cstring << endl; Page / Convert to a basic_string wstring basicstring(orig); basicstring += L" (basic_string)" wcout << basicstr

21、ing << endl; / Convert to a System:String String systemstring = gcnew String(orig); systemstring += " (System:String)" Console:WriteLine("0", systemstring); delete systemstring; 输出 Hello, World! (wchar_t *) Hello, World! (char *) Hello, World! (_bstr_t) Hello, World! (CComB

22、STR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System:String) 从 _bstr_t 转换 示例 说明 此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。 / convert_from_bstr_t.cpp / compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atl

23、base.h" #include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() _bstr_t orig("Hello, World!"); wcout << orig << " (_bstr_t)" << endl; / Convert to a char* const size_t newsize = 100; char nstringn

24、ewsize; strcpy_s(nstring, (char *)orig); strcat_s(nstring, " (char *)"); cout << nstring << endl; / Convert to a wchar_t* wchar_t wcstringnewsize; wcscpy_s(wcstring, (wchar_t *)orig); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; / Conver

25、t to a CComBSTR CComBSTR ccombstr(char *)orig); if (ccombstr.Append(L" (CComBSTR)") = S_OK) Page CW2A printstr(ccombstr); cout << printstr << endl; / Convert to a CString CString cstring(char *)orig); cstring += " (CString)" cout << cstring << endl; / Conv

26、ert to a basic_string string basicstring(char *)orig); basicstring += " (basic_string)" cout << basicstring << endl; / Convert to a System:String String systemstring = gcnew String(char *)orig); systemstring += " (System:String)" Console:WriteLine("0", syste

27、mstring); delete systemstring; 输出 Hello, World! (_bstr_t) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System:String) 从 CComBSTR 转换 示例 说明 此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。 / convert_from_ccombstr.cpp /

28、compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.h" #include "vcclr.h" using namespace std; using namespace System; using namespace Syste

29、m:Runtime:InteropServices; int main() CComBSTR orig("Hello, World!"); CW2A printstr(orig); cout << printstr << " (CComBSTR)" << endl; / Convert to a char* const size_t newsize = 100; char nstringnewsize; CW2A tmpstr1(orig); strcpy_s(nstring, tmpstr1); Page strca

30、t_s(nstring, " (char *)"); cout << nstring << endl; / Convert to a wchar_t* wchar_t wcstringnewsize; wcscpy_s(wcstring, orig); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; / Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bst

31、r_t)" cout << bstrt << endl; / Convert to a CString CString cstring(orig); cstring += " (CString)" cout << cstring << endl; / Convert to a basic_string wstring basicstring(orig); basicstring += L" (basic_string)" wcout << basicstring << en

32、dl; / Convert to a System:String String systemstring = gcnew String(orig); systemstring += " (System:String)" Console:WriteLine("0", systemstring); delete systemstring; 输出 Hello, World! (CComBSTR) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, Wor

33、ld! (CString) Hello, World! (basic_string) Hello, World! (System:String) 从 CString 转换 示例 说明 此示例演示如何从 CString 转换为上面列出的其他字符串类型。 / convert_from_cstring.cpp / compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #

34、include "atlstr.h" #include "comutil.h" using namespace std; using namespace System; int main() CString orig("Hello, World!"); wcout << orig << " (CString)" << endl; Page / Convert to a char* const size_t newsize = 100; char nstringnewsize; s

35、trcpy_s(nstring, orig); strcat_s(nstring, " (char *)"); cout << nstring << endl; / Convert to a wchar_t* / You must first convert to a char * for this to work. size_t origsize = strlen(orig) + 1; size_t convertedChars = 0; wchar_t wcstringnewsize; mbstowcs_s(&convertedChars

36、, wcstring, origsize, orig, _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; / Convert to a _bstr_t _bstr_t bstrt(orig); bstrt += " (_bstr_t)" cout << bstrt << endl; / Convert to a CComBSTR CComBSTR ccombstr(orig); if (ccombstr.A

37、ppend(L" (CComBSTR)") = S_OK) CW2A printstr(ccombstr); cout << printstr << endl; / Convert to a basic_string string basicstring(orig); basicstring += " (basic_string)" cout << basicstring << endl; / Convert to a System:String String systemstring = gcnew St

38、ring(orig); systemstring += " (System:String)" Console:WriteLine("0", systemstring); delete systemstring; 输出 Hello, World! (CString) Hello, World! (char *) PageHello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (basic_string) Hello, World! (S

39、ystem:String) 从 basic_string 转换 示例 说明 此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。 / convert_from_basic_string.cpp / compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "

40、comutil.h" using namespace std; using namespace System; int main() string orig("Hello, World!"); cout << orig << " (basic_string)" << endl; / Convert to a char* const size_t newsize = 100; char nstringnewsize; strcpy_s(nstring, orig.c_str(); strcat_s(nstrin

41、g, " (char *)"); cout << nstring << endl; / Convert to a wchar_t* / You must first convert to a char * for this to work. size_t origsize = strlen(orig.c_str() + 1; size_t convertedChars = 0; wchar_t wcstringnewsize; mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str

42、(), _TRUNCATE); wcscat_s(wcstring, L" (wchar_t *)"); wcout << wcstring << endl; / Convert to a _bstr_t _bstr_t bstrt(orig.c_str(); bstrt += " (_bstr_t)" cout << bstrt << endl; / Convert to a CComBSTR CComBSTR ccombstr(orig.c_str(); if (ccombstr.Append(L&qu

43、ot; (CComBSTR)") = S_OK) CW2A printstr(ccombstr); Page cout << printstr << endl; / Convert to a CString CString cstring(orig.c_str(); cstring += " (CString)" cout << cstring << endl; / Convert to a System:String String systemstring = gcnew String(orig.c_str();

44、systemstring += " (System:String)" Console:WriteLine("0", systemstring); delete systemstring; 输出 Hello, World! (basic_string) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (System:String) 从 System:String 转换 示例 说明 此示例演示如何从 System.String 转换为上面列出的其他字符串类型。 / convert_from_system_string.cpp / compile with /clr /link comsuppw.lib #include <iostream> #include <stdlib.h> #include <string> #include "atlbase.h" #include "atlstr.h" #include "comutil.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论