_itoa

功能:把一整数转换为字符串

用法:char * _itoa(int value, char *string, int radix);

  详细解释: _itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写.其中value为要转化的整数, radix是基数的意思,即先将value转化为radix进制的数,之后再保存在string中.

  备注:该函数的头文件是"stdlib.h"

  程序例:

  #include <stdlib.h>

  #include <stdio.h>

  int main()

  {

  int number = 123456;

  char string[25];

  _itoa(number, string, 10);

  printf("integer = %d string = %s\n", number, string);

  return 0;

  }

  注释:编译系统:VC++6.0,TC不支持。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bufrfish/archive/2008/11/03/3209167.aspx

 

atoi、atof、_itoa、_itow 函数使用

atoi、atof、itoa、itow函数是windows平台下实现字符串与数值相互转换的函数。Linux平台下请使用标准库中的sprintf与sscanf函数。


atoi函数

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

ASCII to integer

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

 

atof函数

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

ASCII to float

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

 

对于以上函数,若字符串无法转化为合法的数值类型,函数将返回0 。

使用范例(来自MSDN):
_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
1_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明#include <stdlib.h>
2_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明#include <stdio.h>
3_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
4_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明void main( void )
5_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明{
6_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   char *s; double x; int i; long l;
7_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
8_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( " testing atoi,atof,atol function :\n" ) ;
9_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    s = "   -2309.12E-15";    /**//* Test of atof */
10_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    x = atof( s );
11_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( "atof test: ASCII string: %s\tfloat:   %e\n", s, x );
12_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
13_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    s = "7.8912654773d210";  /**//* Test of atof */
14_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    x = atof( s );
15_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( "atof test: ASCII string: %s\tfloat:   %e\n", s, x );
16_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
17_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    s = "   -9885 pigs";      /**//* Test of atoi */
18_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    i = atoi( s );
19_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
20_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
21_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    s = "98854 dollars";     /**//* Test of atol */
22_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    l = atol( s );
23_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
24_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明}
25_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明

输出:

atof test: ASCII string:   -2309.12E-15 float: -2.309120e-012

atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210

atoi test: ASCII string:   -9885 pigs    integer: -9885

atol test: ASCII string: 98854 dollars    long: 98854


_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中

    以上2个函数均有安全隐患(当字符数组长度不足保存结果时会导致缓冲区溢出),在vs2008中编译时会有警告。推荐使用它们的安全版本—— _itoa_s与_itow_s 。


_itoa_s 函数原型如下:

errno_t _itoa_s(

   int value,

   char *buffer,

   size_t sizeInCharacters, //存放结果的字符数组长度

   int radix

);

当转换的结果长度比sizeInCharacters变量大时,由于出现access violation,函数将马上终止,而_itoa函数将继续运行。

使用范例(来自MSDN):

_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明Code
1_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明#include<string.h>
2_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明#include<stdlib.h>
3_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明#include<stdio.h>
4_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
5_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明int main( void )
6_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明{
7_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   char buffer[65];
8_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   int r;
9_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( "test _itoa function ! \n" ) ;
10_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   for( r=10; r>=2; --r )
11_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明{
12_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明      _itoa( -1, buffer, r ); // C4996
13_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明     // Note: _itoa is deprecated; consider using _itoa_s instead
14_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明      printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
15_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    }
16_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
17_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    printf( "test _itoa_s function ! \n " ) ;
18_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   for( r=10; r>=2; --r )
19_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明   _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明{
20_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明       _itoa_s( -1, buffer, 65, r );
21_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明       printf( "base %d: %s (%d chars)\n", r, buffer, strnlen(buffer, _countof(buffer)) );
22_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明    }
23_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明}

24_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明

 

输出:

base 10: -1 (2 chars)

base 9: 12068657453 (11 chars)

base 8: 37777777777 (11 chars)

base 7: 211301422353 (12 chars)

base 6: 1550104015503 (13 chars)

base 5: 32244002423140 (14 chars)

base 4: 3333333333333333 (16 chars)

base 3: 102002022201221111210 (21 chars)

base 2: 11111111111111111111111111111111 (32 chars)

 

base 10: -1 (2 chars)

base 9: 12068657453 (11 chars)

base 8: 37777777777 (11 chars)

base 7: 211301422353 (12 chars)

base 6: 1550104015503 (13 chars)

base 5: 32244002423140 (14 chars)

base 4: 3333333333333333 (16 chars)

base 3: 102002022201221111210 (21 chars)

base 2: 11111111111111111111111111111111 (32 chars)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-08-29
  • 2022-12-23
  • 2021-08-24
  • 2021-06-19
猜你喜欢
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2021-10-03
  • 2021-06-11
相关资源
相似解决方案