C中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第1页
C中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第2页
C中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第3页
C中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第4页
C中字符串以及数据类型的转换方法汇总【帖子汇编】.doc_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

C+中数据类型的转换【帖子汇编】在C中有很多的数据类型,在编程的时候,对数据类型进行转换很常遇到的,我在编程过程中频繁遇到甚至头疼,于是在网上网罗各种方法帖子。因为时间有限没法一一整理,遂将各帖汇集一处,与大家共享。希望对大家有帮助。为方便大家区分各帖。每个帖的首页都是另起一页,不会直接跟在后一个帖后面。1、如何将CString变量转换为int变量2、如何将int变量转换CString为变量CStringss=1212.12;inttemp=atoi(ss);CStringaa;aa.Format(%d,temp);3、CString转换为Double4、Double转换为CStringCStringstrValue(1.234);doubledblValue;dblValue=atof(LPCTSTR)strValue);5、char*转换成CString若将char*转换成CString,除了直接赋值外,还可使用CString:Format进行。例如:char chArray = This is a test;char * p = This is a test;或LPSTR p = This is a test;或在已定义Unicode应的用程序中TCHAR * p = _T(This is a test);或LPTSTR p = _T(This is a test);CString theString = chArray;theString.Format(_T(%s), chArray);theString = p;6、CString转换成char*若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:方法一,使用强制转换。例如:CStringtheString(Thisisatest);LPTSTRlpsz=(LPTSTR)(LPCTSTR)theString;方法二,使用strcpy。例如:CStringtheString(Thisisatest);LPTSTRlpsz=newTCHARtheString.GetLength()+1;_tcscpy(lpsz,theString);需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是constwchar_t*(Unicode)或constchar*(ANSI),系统编译器将会自动对其进行转换。方法三,使用CString:GetBuffer。例如:CStrings(_T(Thisisatest);LPTSTRp=s.GetBuffer();/在这里添加使用p的代码if(p!=NULL)*p=_T(0);s.ReleaseBuffer();/使用完后及时释放,以便能使用其它的CString成员函数7、BSTR转换成char*方法一,使用ConvertBSTRToString。例如:#include#pragmacomment(lib,comsupp.lib)int_tmain(intargc,_TCHAR*argv)BSTRbstrText=:SysAllocString(LTest);char*lpszText2=_com_util:ConvertBSTRToString(bstrText);SysFreeString(bstrText);/用完释放deletelpszText2;return0;方法二,使用_bstr_t的赋值运算符重载。例如:_bstr_tb=bstrText;char*lpszText2=b;8、char*转换成BSTR方法一,使用SysAllocString等API函数。例如:BSTRbstrText=:SysAllocString(LTest);BSTRbstrText=:SysAllocStringLen(LTest,4);BSTRbstrText=:SysAllocStringByteLen(Test,4);方法二,使用COleVariant或_variant_t。例如:/COleVariantstrVar(Thisisatest);_variant_tstrVar(Thisisatest);BSTRbstrText=strVar.bstrVal;方法三,使用_bstr_t,这是一种最简单的方法。例如:BSTRbstrText=_bstr_t(Thisisatest);方法四,使用CComBSTR。例如:BSTRbstrText=CComBSTR(Thisisatest);或CComBSTRbstr(Thisisatest);BSTRbstrText=bstr.m_str;方法五,使用ConvertStringToBSTR。例如:char*lpszText=Test;BSTRbstrText=_com_util:ConvertStringToBSTR(lpszText);9、CString转换成BSTR通常是通过使用CStringT:AllocSysString来实现。例如:CStringstr(Thisisatest);BSTRbstrText=str.AllocSysString();SysFreeString(bstrText);/用完释放10、BSTR转换成CString一般可按下列方法进行:BSTRbstrText=:SysAllocString(LTest);CStringAstr;str.Empty();str=bstrText;或CStringAstr(bstrText);11、ANSI、Unicode和宽字符之间的转换方法一,使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。方法二,使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode,而在托管C+环境中还可使用S将ANSI字符串转换成String*对象。例如:TCHARtstr=_T(thisisatest);wchar_twszStr=LThisisatest;String*str=S”Thisisatest”;方法三,使用ATL7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类,它具有如图3所示的统一形式:其中,第一个C表示“类”,以便于ATL3.0宏相区别,第二个C表示常量,2表示“to”,EX表示要开辟一定大小的缓冲。SourceType和DestinationType可以是A、T、W和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。例如,CA2CT就是将ANSI转换成一般类型的字符串常量。下面是一些示例代码:LPTSTRtstr=CA2TEX(thisisatest);LPCTSTRtcstr=CA2CT(thisisatest);wchar_twszStr=LThisisatest;char*chstr=CW2A(wszStr);Visual C+ 如何:在各种字符串类型之间进行转换本主题演示如何将各种 C+ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。从 char * 转换示例说明此示例演示如何从 char * 转换为上面列出的其他字符串类型。/ convert_from_char.cpp/ compile with /clr /link comsuppw.lib#include #include #include #include atlbase.h#include atlstr.h#include comutil.husing 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; const 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); 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 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(0, systemstring); 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 * 转换为上面列出的其他字符串类型。/ convert_from_wchar_t.cpp/ compile with /clr /link comsuppw.lib#include #include #include #include atlbase.h#include atlstr.h#include comutil.husing namespace std;using namespace System;int main() wchar_t *orig = LHello, 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); 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 printstr(ccombstr); cout printstr 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 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! (CComBSTR)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 #include #include #include atlbase.h#include atlstr.h#include comutil.husing 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 nstringnewsize; 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; / Convert to a CComBSTR CComBSTR ccombstr(char *)orig); if (ccombstr.Append(L (CComBSTR) = S_OK) CW2A printstr(ccombstr); cout printstr endl; / Convert to a CString CString cstring(char *)orig); cstring += (CString); cout cstring endl; / Convert 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, systemstring); 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/ compile with /clr /link comsuppw.lib#include #include #include #include atlbase.h#include atlstr.h#include comutil.h#include vcclr.husing namespace std;using namespace System;using namespace System: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); strcat_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 += (_bstr_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 endl; / 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, World! (CString)Hello, World! (basic_string)Hello, World! (System:String)从 CString 转换示例说明此示例演示如何从 CString 转换为上面列出的其他字符串类型。/ convert_from_cstring.cpp/ compile with /clr /link comsuppw.lib#include #include #include #include atlbase.h#include atlstr.h#include comutil.husing namespace std;using namespace System;int main() CString orig(Hello, World!); wcout orig (CString) endl; / Convert to a char* const size_t newsize = 100; char nstringnewsize; strcpy_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, 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.Append(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 String(orig); systemstring += (System:String); Console:WriteLine(0, systemstring); delete systemstring;输出Hello, World! (CString)Hello, World! (char *)Hello, World! (wchar_t *)Hello, World! (_bstr_t)Hello, World! (CComBSTR)Hello, World! (basic_string)Hello, World! (System:String)从 basic_string 转换示例说明此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。/ convert_from_basic_string.cpp/ compile with /clr /link comsuppw.lib#include #include #include #include atlbase.h#include atlstr.h#include comutil.husing 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(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.c_str() + 1; size_t convertedChars = 0; wchar_t wcstringnewsize; mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _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 (CComBSTR) = S_OK) CW2A printstr(ccombstr); 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(); 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 #include #include #include atlbase.h#include atlstr.h#include comutil.h#include vcclr.husing namespace std;using namespace System;using namespace System:Runtime:InteropServices;int main() String orig = gcnew String(Hello, World!); Console:WriteLine(0 (System:String), orig); pin_ptr wch = PtrToStringChars(orig); / Convert to a char* size_t origsize = wcslen(wch) + 1; const size_t newsize = 100; size_t convertedChars = 0; char nstringnewsize; wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE); strcat_s(nstring, (char *); cout nstring endl; / Convert to a wchar_t* wchar_t wcstringnewsize; wcscpy_s(wcstring, wch); wcscat_s(wcstring, L (wchar_t *); wcout wcstring endl; / Convert to a _bstr_t _bstr_t bstrt(wch); bstrt += (_bstr_t); cout bstrt endl; / Convert to a CComBSTR CComBSTR ccombstr(wch); if (ccombstr.Append(L (CComBSTR) = S_OK) CW2A printstr(ccombstr); cout printstr endl; / Convert to a CString CString cstring(wch); cstring += (CString); cout cstring endl; / Convert to a basic_string wstring basicstring(wch); basicstring += L (basic_string); wcout basicstring endl; delete orig;输出通过阅读本文你可以学习如何有效地使用 CStringCString 是一种很有用的数据类型。它们很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作的时候方便了很多。不管怎样,使用CString有很多特殊的技巧,特别是对于纯C背景下走出来的程序员来说有点难以学习。这篇文章就来讨论这些技巧。使用CString可以让你对字符串的操作更加直截了当。这篇文章不是CString的完全手册,但囊括了大部分常见基本问题。这篇文章包括以下内容:CString 对象的连接 格式化字符串(包括 int 型转化为 CString ) CString 型转化成 int 型 CString 型和 char* 类型的相互转化char* 转化成 CString CString 转化成 char* 之一:使用LPCTSTR强制转化 CString 转化成 char* 之二:使用CString对象的GetBuffer方法 CString 转化成 char* 之三: 和控件的接口CString 型转化成 BSTR 型; BSTR 型转化成 CString 型; VARIANT 型转化成 CString 型; 载入字符串表资源; CString 和临时对象; CString 的效率; 总结 下面我分别讨论。 1、CString 对象的连接能体现出 CString 类型方便性特点的一个方面就字符串的连接,使用 CString 类型,你能很方便地连接两个字符串,正如下面的例子:CString gray(Gray);CString cat(Cat);CString graycat = gray + cat;要比用下面的方法好得多:char gray = Gray;char cat = Cat;char * graycat = malloc(strlen(gray) + strlen(cat) + 1);strcpy(graycat, gray);strcat(graycat, cat); 2、格式化字符串与其用 sprintf() 函数或 wsprintf() 函数来格式化一个字符串,还不如用 CString 对象的Format()方法:CString s;s.Format(_T(The total is %d), total);用这种方法的好处是你不用担心用来存放格式化后数据的缓冲区是否足够大,这些工作由CString类替你完成。格式化是一种把其它不是字符串类型的数据转化为CString类型的最常用技巧,比如,把一个整数转化成CString类型,可用如下方法:CString s;s.Format(_T(%d), total);我总是对我的字符串使用_T()宏,这是为了让我的代码至少有Unicode的意识,当然,关于Unicode的话题不在这篇文章的讨论范围。_T()宏在8位字符环境下是如下定义的:#define _T(x) x / 非Unicode版本(non-Unicode version)而在Unicode环境下是如下定义的:#define _T(x) L#x / Unicode版本(Unicode version)所以在Unicode环境下,它的效果就相当于:s.Format(L%d, total);如果你认为你的程序可能在Unicode的环境下运行,那么开始在意用 Unicode 编码。比如说,不要用 sizeof() 操作符来获得字符串的长度,因为在Unicode环境下就会有2倍的误差。我们可以用一些方法来隐藏Unicode的一些细节,比如在我需要获得字符长度的时候,我会用一个叫做DIM的宏,这个宏是在我的dim.h文件中定义的,我会在我写的所有程序中都包含这个文件:#define DIM(x) ( sizeof(x) / sizeof(x)0) )这个宏不仅可以用来解决Unicode的字符串长度的问题,也可以用在编译时定义的表格上,它可以获得表格的项数,如下:class Whatever . ;Whatever data = . , . . ,;for(int i = 0; i DIM(data); i+) / 扫描表格寻找匹配项。这里要提醒你的就是一定要注意那些在参数中需要真实字节数的API函数调用,如果你传递字符个数给它,它将不能正常工作。如下:TCHAR data20;lstrcpyn(data, longstring, sizeof(data) - 1); / WRONG!lstrcpyn(data, longstring, DIM(data) - 1); / RIGHTWriteFile(f, data, DIM(data), &bytesWritten, NULL); / WRONG!WriteF

温馨提示

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

评论

0/150

提交评论