CString转int _ttoi() _tcstoul() Format ASSERT() Format的用法

来源:互联网 发布:北京和隆优化 编辑:程序博客网 时间:2024/06/05 00:53

转载地址:http://www.cnblogs.com/cyrix/articles/1744447.html

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 4294967294 
  f 对应浮点数 
  e 科学表示法,对应整型数和浮点数, 
  比如Format("this is %e",-2.22); 
  返回的是:this is -2.220000E+000 
  等一下再说明如果将数的精度缩小 
  g 这个只能对应浮点型,且它会将值中多余的数去掉 
  比如Format("this is %g",02.200); 
  返回的是:this is 2.2 
  n 只能对应浮点型,将值转化为号码的形式。看一个例子就明白了 
  Format("this is %n",4552.2176); 
  返回的是this is 4,552.22 
  注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况 
  二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的 
  m 钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化 
  另外它只对应于浮点值 
  Format("this is %m",9552.21); 
  返回:this is ¥9,552.21 
  p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示 
  例如: 
  Format("this is %p",p); 
  Edit1的内容是:this is 0012F548 
  s 对应字符串类型,不用多说了吧 
  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.12 
  Format('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:40 
  d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位 
  FormatdateTime('d',now); 
  输出可能为1~31 
  dd 和d的意义一样,但它始终是以两位来显示的 
  FormatdateTime('dd',now); 
  输出可能为01~31 
  ddd 显示的是星期几 
  FormatdateTime('ddd',now); 
  输出为: 星期六 
  dddd 和ddd显示的是一样的。 
  但上面两个如果在其他国家可能不一样。 
  ddddd 以短时间格式显示年月日 
  FormatdateTime('ddddd',now); 
  输出为:2004-8-7 
  dddddd 以长时间格式显示年月日 
  FormatdateTime('dddddd',now); 
  输出为:2004年8月7日 
  e/ee/eee/eeee 以相应的位数显示年 
  FormatdateTime('ee',now); 
  输出为:04 (表示04年) 
  m/mm/mmm/mmmm 表示月 
  FormatdateTime('m',now); 
  输出为:8 
  FormatdateTime('mm',now); 
  输出为 08 
  FormatdateTime('mmm',now); 
  输出为 八月 
  FormatdateTime('mmmm',now); 
  输出为 八月 
  和ddd/dddd 一样,在其他国家可能不同 
  yy/yyyy 表示年 
  FormatdateTime('yy',now); 
  输出为 04 
  FormatdateTime('yyyy',now); 
  输出为 2004 
  h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒 
  t 以短时间格式显示时间 
  FormatdateTime('t',now); 
  输出为 10:17 
  tt 以长时间格式显示时间 
  FormatdateTime('tt',now); 
  输出为10:18:46 
  ampm 以长时间格式显示上午还是下午 
  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.00 
  
  E 科学表示法,看几个例子大概就明白了 
  FormatFloat('0.00E+00',2222.22); 
  输出是 2.22E+03 
  FormatFloat('0000.00E+00',2222.22); 
  输出是 2222.22E+00 
  FormatFloat('00.0E+0',2222.22); 
  22.2E+2 
  明白了吗,全靠E右边的0来支配的。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ANSI 和 UNICODE 的函数对应表


ANSI        UNICODE     通用
(char.h)    (wchar.h)        (tchar.h)

char         wchar_t          TCHAR
char *      wchar_t *        PTCHAR (PTSTR,LPWSTR,PWSTR,WCHAR)

printf       wprintf          _tprintf
scanf       wscanf          _tscanf

atoi         _wtoi            _ttoi
atol         _wtol            _ttol
itoa         _itow            _itot
ltoa         _ltow            _ltot

atof         _wtof            _tstof

strlen       wcslen          _tcslen
strcat       wcscat          _tcscat
strcpy      wcscpy         _tcscpy
strcmp     wcscmp        _tcscmp

 

 

 

ASCII

UNICODE

TCHAR

VS2005

int

atoi

_wtoi

_tstoi

_ttoi 

_atoi_l

_wtoi_l

long

atol

_wtol

_tstoi

_ttoi

_atoi_l

_wtoi_l

__int64

_atoi64

_wtoi64

_tstoi64

_ttoi64

_atoi64_l

_wtoi64_l

float

 

 

 

_atoflt

_atoflt_l 

double

atof

_wtof

_tstof

_ttof

_atof_l

_wtof_l

_atodbl

_atodbl_l

long double

 

 

 

_atoldbl

_atoldbl_l 

atof, _atof_l, _wtof, _wtof_l

Convert string to float

atoi, _atoi_l, _wtoi, _wtoi_l

Convert string to int

_atoi64, _atoi64_l, _wtoi64, _wtoi64_l

Convert string to __int64

atol, _atol_l, _wtol, _wtol_l

Convert string to long

_ecvt, _ecvt_s

Convert double to string of specified length

_fcvt, _fcvt_s

Convert double to string with specified number of digits following decimal point

_gcvt, _gcvt_s

Convert 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_s

Convert int or __int64 to string

_ltoa, _ltow, _ltoa_s, _ltow_s

Convert long to string

strtod, _strtod_l, wcstod, _wcstod_l

Convert string to double

strtol, wcstol, _strtol_l, _wcstol_l

Convert string to long integer

strtoul, _strtoul_l, wcstoul, _wcstoul_l

Convert string to unsigned long integer

_ultoa, _ultow, _ultoa_s, _ultow_s

Convert unsigned long to string

atof, _atof_l, _wtof, _wtof_l

Convert wide-character string to a double

atoi, _atoi_l, _wtoi, _wtoi_l

Convert wide-character string to int

_atoi64, _atoi64_l, _wtoi64, _wtoi64_l

Convert wide-character string to __int64

atol, _atol_l, _wtol, _wtol_l

Convert 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继续调试。   


0 0