CString 类型转换大全.doc_第1页
CString 类型转换大全.doc_第2页
CString 类型转换大全.doc_第3页
CString 类型转换大全.doc_第4页
CString 类型转换大全.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

CString 类型转换大全首页CString转int _ttoi() _tcstoul() Format ASSERT() 作者: tags:Form for TC ORM String int CString assert str sse ing for in assert int _ttoi() _tcstoul() Format ASSERT() -CString 型转化成 int 型把 CString 类型的数据转化成整数类型最简单的方法就是使用标准的字符串到整数转换例程。虽然通常你怀疑使用_atoi()函数是一个好的选择,它也很少会是一个正确的选择。假如你预备使用 Unicode 字符,你应该用_ttoi(),它在 ANSI 编码系统中被编译成_atoi(),而在 Unicode 编码系统中编译成_wtoi()。你也可以考虑使用_tcstoul()或者_tcstol(),它们都能把字符串转化成任意进制的长整数(如二进制、八进制、十进制或十六进制),不同点在于前者转化后的数据是无符号的(unsigned),而后者相反。看下面的例子:CString hex = _T(FAB);CString decimal = _T(4011);ASSERT(_tcstoul(hex, 0, 16) = _ttoi(decimal); 在做DWORD与 CString 比较的时候 ,需要将CString 转换成 DWORD,有几种转换方法,但是明明转换结果是一样的,可就是提示不相等,后来 _tcstoul()转换后才相等 ,记录下 CString str = lp-GetSubItem(nCol); / MessageBox(str,m_strItem,MB_OK); DWORD dwData = _tcstoul( str, NULL, 10 ); / 10进制 if (dwItem =dwData) /AfxMessageBox(找到找到找到找到找到找到找到找到找到找到找到找到找到); return pCur; 使用_tcstoul()或者_tcstol(),它们都能把字符串转化成任意进制的长整数(如二进制、八进制、十进制或十六进制),不同点在于前者转化后的数据是无符号的(unsigned),而后者相反。看下面的例子:CString hex = _T(FAB);CString decimal = _T(4011);ASSERT(_tcstoul(hex, 0, 16) = _ttoi(decimal);/Format函数VC+中Format函数详解首先看它的声明:function Format(const Format: string; const Args: array of const): string; overload;事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,但并不多用,所以这里只对第一个介绍:function Format(const Format: string; const Args: array of const): string; overload;Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。如以下例子:Format(my name is %6s,wind);返回后就是my name is wind现在来看Format参数的具体情况:Format里面可以写普通的字符串,比如my name is但有些格式指令字符具有非凡意义,比如%6s格式指令具有以下的形式:% index : - width . prec type它是以%开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,是可选的。先来看看type,type可以是以下字符:d十制数,表示一个整型值u和d一样是整型值,但它是无符号的,而假如它对应的值是负的,则返回时是一个2的32次方减去这个绝对值的数如:Format(this is %u,-2);返回的是:this is 4294967294f对应浮点数e科学表示法,对应整型数和浮点数,比如Format(this is %e,-2.22);返回的是:this is -2.220000E+000等一下再说明假如将数的精度缩小g这个只能对应浮点型,且它会将值中多余的数去掉比如Format(this is %g,02.200);返回的是:this is 2.2n只能对应浮点型,将值转化为号码的形式。看一个例子就明白了Format(this is %n,4552.2176);返回的是this is 4,552.22注重有两点,一是只表示到小数后两位,等一下说怎么消除这种情况二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的m钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化另外它只对应于浮点值Format(this is %m,9552.21);返回:this is ¥9,552.21p对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示例如:Format(this is %p,p);Edit1的内容是:this is 0012F548s对应字符串类型,不用多说了吧x必须是一个整形值,以十六进制的形式返回Format(this is %X,15);返回是:this is F类型讲述完毕,下面介绍格式化Type的指令:index :这个要怎么表达呢,看一个例子Format(this is %d %d,12,13);其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候是这样this is 12 13而假如你这样定义:Format(this is %1:d %0:d,12,13);那么返回的字符串就变成了this is 13 12现在明白了吗,index : 中的index指示Args中参数显示的顺序还有一种情况,假如这样Format(%d %d %d %0:d %d, 1, 2, 3, 4) ;将返回1 2 3 1 2。假如你想返回的是1 2 3 1 4,必须这样定:Format(%d %d %d %0:d %3:d, 1, 2, 3, 4) ;但用的时候要注重,索引不能超出Args中的个数,不然会引起异常如Format(this is %2:d %0:d,12,13);由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了width指定将被格式化的值占的宽度,看一个例子就明白了Format(this is %4d,12);输出是:this is 12这个是比较轻易,不过假如Width的值小于参数的长度,则没有效果。如:Format(this is %1d,12);输出是:this is 12-这个指定参数向左齐,和width合在一起最可以看到效果:Format(this is %-4d,yes,12);输出是:this is 12 ,yes. prec指定精度,对于浮点数效果最佳:Format(this is %.2f,1.1234);输出 this is 1.12Format(this is %.7f,1.1234);输了 this is 1.1234000而对于整型数,假如prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之Format(this is %.7d,1234);输出是:this is 0001234对于字符型,刚好和整型值相反,假如prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符Format(this is %.2s,1234);输出是 this is 12而上面说的这个例子:Format(this is %e,-2.22);返回的是:this is -2.22000000000000E+000怎么去掉多余的0呢,这个就行啦Format(this is %.2e,-2.22);好了,第一个总算讲完了,应该对他的应用很熟知了吧/二 FormatDateTime的用法他的声明为:function FormatDateTime(const Format: string; DateTime: TDateTime): string;overload;当然和Format一样还有一种,但这里只介绍常用的第一种Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串重点来看Format参数中的指令字符c 以短时间格式显示时间,即全部是数字的表示FormatdateTime(c,now);输出为:2004-8-7 9:55:40d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位FormatdateTime(d,now);输出可能为131dd 和d的意义一样,但它始终是以两位来显示的FormatdateTime(dd,now);输出可能为0131ddd 显示的是星期几FormatdateTime(ddd,now);输出为: 星期六dddd 和ddd显示的是一样的。但上面两个假如在其他国家可能不一样。ddddd 以短时间格式显示年月日FormatdateTime(ddddd,now);输出为:2004-8-7dddddd 以长时间格式显示年月日FormatdateTime(dddddd,now);输出为:2004年8月7日e/ee/eee/eeee 以相应的位数显示年FormatdateTime(ee,now);输出为:04 (表示04年)m/mm/mmm/mmmm 表示月FormatdateTime(m,now);输出为:8FormatdateTime(mm,now);输出为 08FormatdateTime(mmm,now);输出为 八月FormatdateTime(mmmm,now);输出为 八月和ddd/dddd 一样,在其他国家可能不同yy/yyyy 表示年FormatdateTime(yy,now);输出为 04FormatdateTime(yyyy,now);输出为 2004h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒t 以短时间格式显示时间FormatdateTime(t,now);输出为 10:17tt 以长时间格式显示时间FormatdateTime(tt,now);输出为10:18:46ampm 以长时间格式显示上午还是下午FormatdateTime(ttampm,now);输出为:10:22:57上午大概如此,假如要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中假如含非凡的字符就不会被显示为时间格式啦:FormatdateTime(today is c,now);输出为:today is 2004-8-7 10:26:58时间中也可以加-或来分开日期:FormatdateTime(today is yy-mm-dd,now);FormatdateTime(today is yymmdd,now);输出为: today is 04-08-07也可以用:来分开时间FormatdateTime(today is hh:nn:ss,now);输出为:today is 10:32:23/三.FormatFloat的用法常用的声明:function FormatFloat(const Format: string; Value: Extended): string; overload;和上面一样Format参数为格式化指令字符,Value为Extended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,假如传入该方法的参数比如Double或者其他,则可以保存不会超出范围。要害是看Format参数的用法0 这个指定相应的位数的指令。比如:FormatFloat(000.000,22.22);输出的就是022.220注重一点,假如整数部分的0的个数小于Value参数中整数的位数,则没有效果如:FormatFloat(0.00,22.22);输出的是:22.22但假如小数部分的0小于Value中小数的倍数,则会截去相应的小数和位数如:FormatFloat(0.0,22.22);输出的是:22.2也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出句FormatFloat(0,000.0,2222.22);输出是:2,222.2假如这样FormatFloat(000,0.0,2222.22);它的输出还是:2,222.2注重它的规律# 和0的用法一样,目前我还没有测出有什么不同。FormatFloat(#.#,22.22);输出是:22.00E 科学表示法,看几个例子大概就明白了FormatFloat(0.00E+00,2222.22);输出是 2.22E+03FormatFloat(0000.00E+00,2222.22);输出是 2222.22E+00FormatFloat(00.0E+0,2222.22);22.2E+2明白了吗,全靠E右边的0来支配的。/ANSI 和 UNICODE 的函数对应表 ANSI UNICODE通用(char.h) (wchar.h) (tchar.h)char wchar_t TCHARchar * wchar_t * PTCHAR (PTSTR,LPWSTR,PWSTR,WCHAR)printf wprintf _tprintfscanf wscanf_tscanfatoi _wtoi _ttoiatol _wtol _ttolitoa _itow _itotltoa _ltow _ltotatof _wtof _tstofstrlen wcslen_tcslenstrcat wcscat _tcscatstrcpy wcscpy_tcscpystrcmpwcscmp_tcscmpASCIIUNICODETCHARVS2005intatoi_wtoi_tstoi_ttoi_atoi_l_wtoi_llongatol_wtol_tstoi_ttoi_atoi_l_wtoi_l_int64_atoi64_wtoi64_tstoi64_ttoi64_atoi64_l_wtoi64_lfloat_atoflt_atoflt_ldoubleatof_wtof_tstof_ttof_atof_l_wtof_l_atodbl_atodbl_llong double_atoldbl_atoldbl_latof, _atof_l, _wtof, _wtof_lConvert string to floatatoi, _atoi_l, _wtoi, _wtoi_lConvert string to int_atoi64, _atoi64_l, _wtoi64, _wtoi64_lConvert string to _int64atol, _atol_l, _wtol, _wtol_lConvert string to long_ecvt, _ecvt_sConvert double to string of specified length_fcvt, _fcvt_sConvert double to string with specified number of digits following decimal point_gcvt, _gcvt_sConvert double number to string; store string in buffer_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow, _itoa_s, _i64toa_s, _ui64toa_s, _itow_s, _i64tow_s, _ui64tow_sConvert int or _int64 to string_ltoa, _ltow, _ltoa_s, _ltow_sConvert long to stringstrtod, _strtod_l, wcstod, _wcstod_lConvert string to doublestrtol, wcstol, _strtol_l, _wcstol_lConvert string to long integerstrtoul, _strtoul_l, wcstoul, _wcstoul_lConvert string to unsigned long integer_ultoa, _ultow, _ultoa_s, _ultow_sConvert unsigned long to stringatof, _atof_l, _wtof, _wtof_lConvert wide-character string to a doubleatoi, _atoi_l, _wtoi, _wtoi_lConvert wide-character string to int_atoi64, _atoi64_l, _wtoi64, _wtoi64_lConvert wide-character string to _int64atol, _atol_l, _wtol, _wtol_lConvert wide-character string to long/ASSERT()是一个调试程序时经常使用的宏,在程序运行时它计算括号内的表达式,假如表达式为FALSE (0), 程序将报告错误,并终止执行。假如表达式不为0,则继续执行后面的语句。这个宏通常原来判定程序中是否出现了明显非法的数据,假如出现了终止程序以免导致严重后果,同时也便于查找错误。 ASSERT只有在Debug版本中才有效,假如编译为Release版本则被忽略。 - ASSERT宏定义如下 #define ASSERT(f) do if (!(f) & AfxAssertFailedLine(THIS_FILE, _LINE_) AfxDebugBreak(); while (0) ASSERT(逻辑表达式) 假如括号中的逻辑表达式值为假的话,会弹出调试命令窗口,提示具体在哪个文件的哪一行发生了断言错误! - ASSERT Evaluates an expression, and displays a diagnostic message if the expression is FALSE. Ignored in retail builds. Syntax ASSERT( cond ); Parameters cond Expression to evaluate. Remarks In debug builds, if the expression is FALSE, this macro displays a message box with the text of the expression, the name of the source file, and the line number. The user can ignore the assertion, enter the debugger, or quit the application. Example ASSERT(rtStartTime = rtEndTime); - 断言(ASSERT)的使用,方法很简单。为什么要用,初学者可能比较迷惑。 契约式编程讲的比较清楚,建议可以先看看这类书。 一个函数由前置条件、后置条件和不变式组成。在VC中,我们可以通过断言来保证这三个条件。可以大大提高了软件的质量。 - 假如ASSERT()中的条件不成立(比如 ASSERT(0) ; ),会弹出一个比较吓人的对话框。 点击重试,可以到达 ASSERT 断言不成立的那一行, 此时可以在watch窗口查看变量值,找出出错的原因。 假如程序能够继续运行,可以按F5继续调试。怎么从CString 转换到 char *CString str(abc); char *str1; str1=(LPSTR)(LPCTSTR)str;应该得行!c+中char* string CString的互相转换 收藏 #include /使用C+标准库的string类时using namespace std; /同上#include #include #include /要将string类和int类型直接转换最好有这些包含, /因为自己写一个转换函数比较方便,函数定义参考如下string getstring ( const int n ) std:stringstream newstr; newstr CStringCString.format(”%s”, string.c_str();用c_str()确实比data()要好.2,char - stringstring s(char *);只能初始化,在不是初始化的地方最好还是用assign().3,CString - stringstring s(CString.GetBuffer();GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.C+标准函数库中说的有三个函数可以将字符串的内容转换为字符数组和Cstring1.data(),返回没有”0“的字符串数组2,c_str(),返回有”0“的字符串数组3,copy()CString

温馨提示

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

评论

0/150

提交评论