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

原文:http://www.cnblogs.com/lidabo/archive/2012/07/10/2584706.html

_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):


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

输出:

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):

Code

输出:

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)

时间: 2024-08-09 22:47:53

_itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明的相关文章

atoi、itoa,strcpy,strcmp,memcpy等实现

原文:http://www.cnblogs.com/lpshou/archive/2012/06/05/2536799.html 1.memcpy.memmove.memset源码 link:http://note.youdao.com/share/?id=1f826e4337c7db272e94fdb4f267a8de&type=note 2.strcpy.strcat等源码 link:http://note.youdao.com/share/?id=d23a598b2e31321517ed5

c++实现atoi()和itoa()函数(字符串和整数转化)

  c++实现atoi()和itoa()函数(字符串和整数转化) 一:起因 (1)字符串类型转化为整数型(Integer),还是字符串类型(String)转化为Double类型,这在java里面有非常好的内部函数,很easy的事情: (2)但是在c里面没有Integer Double等包装类,由char[]数组转化为整数型就变得不那么简单了,atoi()  itoa()在widows下面有,但是网上说linux 下好像没有 itoa() 函数,用 sprintf() 好了,但是本人测试了一下sp

实现atoi和itoa

1 /******************************** 2 * 实现atoi和itoa 3 ********************************/ 4 #include <stdio.h> 5 #include <string.h> 6 #include <stdlib.h> 7 //将字符串转化为整数 8 int chartoi(char a) 9 { 10 if(a >= '0' || a <= '9') 11 return

atoi(),itoa() ,atol(),atof ()的实现

atoi(将字符串转换成整型数):atoi(const char *nptrnt ) 函数说明 : atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回. itoa(将整型数转换成字符串):char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据. string:目标字符串的地址. radix:转换后的进制数,可以是

常用的函数:atoi,itoa,atof,_ttoi等

出自http://blog.csdn.net/zzyoucan/article/details/10260093 atoi---ASCII to integer,将字符串转换成整形,从数字或正负号开始转换,一直到非数字为止 #include <stdlib.h> #include <stdio.h> int main(void) { float n; char *str = "12345.67"; n = atoi(str); printf("stri

atoi 和itoa用法

1.itoa 在linux下没有itoa这个函数       原型:char  *itoa(int   value,char   *string,int   radix)          用法:#include           功能:将整数value转换成字符串存入string, radix为转换时所用基数(保存到字符串中的数据的进制基数 2 8 10 16)             说明:返回指向转换后的字符串的指针              举例: #include    #inclu

剑指offer 把数组排成最小的数 atoi和itoa,pow

pow(x,y)在#include<math.h>文件中,计算x的y次方. C++引入头文件:#include <stdlib.h> 或者 #include <cstdlib> 1.整数转化为字符串的方法: 1.1 atoi原型:注意:参数若为string类型一定转换成char*型(str.c_str()) #include <stilib.h>或者#include<cstdlib> int atoi(const char *str); atoi

atoi atof atol

在c语言中提供了把字符串转化为整数的函数,并没有提供把整数转化为字符串的函数 atoi是标准的库函数 itoa不是标准的库函数(在vs可编译,其它系统中未知) atol把一个字符串转化为long类型 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main() 6 { 7 char a[100] = "123"; 8 char b[100] = "4

关于atoi,itoa与itob的重写和字符统计

首先关于函数atoi的重写, atoi的功能是字符串能够转换为整数保存,仅仅针对于整数,浮点数以后会有写: //实现一个函数int my_atoi(char s[]),可以将一个字符串转换为对应的整数. #include <stdio.h> #include <ctype.h> int main() { char st[50]; gets(st);  printf("%d",atoi(st)); return 0; } int atoi(char s[]) {