您的位置:首页 > 其它

字符串与数值相互转换的函数

2010-12-11 15:42 411 查看
字符串与数值相互转换的函数



atoi、atof、itoa、itow函数是windows平台下实现字符串与数值相互转换的函数【参考



atoi函数

原型:int atoi( const char *string );

ASCII to integer

作用:将字符串转为integer类型



atof函数

原型:double atof( const char *string );

ASCII to float

作用:将字符串转为double类型



_itoa函数

原型:char *_itoa( int value, char *str, int radix );//2<=radix<=36

Integer to ASCII

作用:将Integer类型转换为radix进制,然后以ASCII字符串的形式存放在str中



_itow函数

wchar_t * _itow( int value, wchar_t *str, int radix ); //2<=radix<=36

Integer to Wide Char

作用:将Integer类型转换为radix进制,然后以宽字符串的形式存放在str中



atol等省略

自从VS2005版本以后,都有了更加严格的转换函数了, 如itoa > _itoa > _itoa_s(推荐)



_gcvt_s

Converts a floating-point value to a string. This is a version of _gcvt with security enhancements as described in Security Features in the CRT.

errno_t _gcvt_s( 
   char *buffer,
   size_t sizeInBytes,
   double value,
   int digits 
);
template <size_t cchStr>
errno_t _gcvt_s( 
   char (&buffer)[cchStr],
   double value,
   int digits 
); // C++ only

sample:

[code]// crt_gcvt_s.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main()
{
  char buf[_CVTBUFSIZE];
  int decimal;
  int sign;
  int err;

  err = _gcvt_s(buf, _CVTBUFSIZE, 1.2, 5);
  
  if (err != 0)
  {
     printf("_gcvt_s failed with error code %d/n", err);
     exit(1);
  }

  printf("Converted value: %s/n", buf);  
  
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: