如何转换字符串为数值- -

MFC中的CString和STL中的string似乎并没有提供相关的函数将字符串转换为数值。

以下是一些可能用到的函数:
atoi atol atof 
strtol strtoul strtod

以下代码演示了前三个函数的用法:
#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char *s; double x; int i; long l;

   s = "  -2309.12E-15";    /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s/tfloat:  %e/n", s, x );

   s = "7.8912654773d210";  /* Test of atof */
   x = atof( s );
   printf( "atof test: ASCII string: %s/tfloat:  %e/n", s, x );

   s = "  -9885 pigs";      /* Test of atoi */
   i = atoi( s );
   printf( "atoi test: ASCII string: %s/t/tinteger: %d/n", s, i );

   s = "98854 dollars";     /* Test of atol */
   l = atol( s );
   printf( "atol test: ASCII string: %s/t/tlong: %ld/n", s, l );
}

以下代码演示了后三个函数的用法:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
   char   *string, *stopstring;
   double x;
   long   l;
   int    base;
   unsigned long ul;
   string = "3.1415926This stopped it";
   x = strtod( string, &stopstring );
   printf( "string = %s/n", string );
   printf("   strtod = %f/n", x );
   printf("   Stopped scan at: %s/n/n", stopstring );
   string = "-10110134932This stopped it";
   l = strtol( string, &stopstring, 10 );
   printf( "string = %s", string );
   printf("   strtol = %ld", l );
   printf("   Stopped scan at: %s", stopstring );
   string = "10110134932";
   printf( "string = %s/n", string );
   /* Convert string using base 2, 4, and 8: */
   for( base = 2; base <= 8; base *= 2 )
   {
      /* Convert the string: */
      ul = strtoul( string, &stopstring, base );
      printf( "   strtol = %ld (base %d)/n", ul, base );
      printf( "   Stopped scan at: %s/n", stopstring );
   }
 return 0;
}

更详细的数据转换函数如下:
abs Find absolute value of integer
atof Convert string to float
atoi, _atoi64 Convert string to int
atol Convert string to long
_ecvt Convert double to string of specified length
_fcvt Convert double to string with specified number of digits following decimal point
_gcvt Convert double number to string; store string in buffer
_itoa, _i64toa, _itow, _i64tow Convert int to string
labs Find absolute value of long integer
_ltoa, _ltow Convert long to string
_mbbtombc Convert 1-byte multibyte character to corresponding 2-byte multibyte character
_mbcjistojms Convert Japan Industry Standard (JIS) character to Japan Microsoft (JMS) character
_mbcjmstojis Convert JMS character to JIS character
_mbctohira Convert multibyte character to 1-byte hiragana code
_mbctokata Convert multibyte character to 1-byte katakana code
_mbctombb Convert 2-byte multibyte character to corresponding 1-byte multibyte character
mbstowcs Convert sequence of multibyte characters to corresponding sequence of wide characters
mbtowc Convert multibyte character to corresponding wide character
strtod, wcstod Convert string to double
strtol, wcstol Convert string to long integer
strtoul, wcstoul Convert string to unsigned long integer
strxfrm, wcsxfrm Transform string into collated form based on locale-specific information
__toascii Convert character to ASCII code
tolower, towlower, _mbctolower Test character and convert to lowercase if currently uppercase
_tolower Convert character to lowercase unconditionally
toupper, towupper, _mbctoupper Test character and convert to uppercase if currently lowercase
_toupper Convert character to uppercase unconditionally
_ultoa, _ultow Convert unsigned long to string
wcstombs Convert sequence of wide characters to corresponding sequence of multibyte characters
wctomb Convert wide character to corresponding multibyte character
_wtoi Convert wide-character string to int
_wtol Convert wide-character string to long

 

- 作者: robindy 2005年04月28日, 星期四 09:24 加入博采

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=1352094

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值