itoa函数 atoi函数

本文详细介绍了C语言中用于数字和字符串相互转换的函数,包括atoi和itoa函数的具体实现与使用方法,并通过示例代码展示了如何将整数转换为字符串及如何将字符串解析为整数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://blog.youkuaiyun.com/ouyangzp/article/details/2570376

 

itoa函数及atoi函数 
C语言提供了几个标准库函数,可以将任意类型(整型、长整型、浮点型等)的数字转换为字符串。以下是用itoa()函数将整数转 换为字符串的一个例子: 

# include <stdio.h> 
# include <stdlib.h> 

void main (void) 
{ 
int num = 100; 
char str[25]; 
itoa(num, str, 10); 
printf("The number 'num' is %d and the string 'str' is %s. /n" , 
num, str); 
} 

itoa()函数有3个参数:第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转移数字时所用 的基数。在上例中,转换基数为10。10:十进制;2:二进制... 
itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。 
是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似: 

char str[255]; 
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。 

下列函数可以将整数转换为字符串: 
---------------------------------------------------------- 
函数名 作 用 
---------------------------------------------------------- 
itoa() 将整型值转换为字符串 
itoa() 将长整型值转换为字符串 
ultoa() 将无符号长整型值转换为字符串 

一  atoi     把字符串转换成整型数 
例程序: 
#include <ctype.h> 
#include <stdio.h> 
int atoi (char s[]); 

int main(void ) 
{   
char s[100]; 
gets(s); 
printf("integer=%d/n",atoi(s)); 
return 0; 
} 
int atoi (char s[]) 
{ 
int i,n,sign; 
for(i=0;isspace(s[i]);i++)//跳过空白符 
      ; 
sign=(s[i]=='-')?-1:1; 
if(s[i]=='+'||s[i]==' -')//跳过符号 
      i++; 
for(n=0;isdigit(s[i]);i++) 
      n=10*n+(s[i]-'0');//将数字字符转换成整形数字 
return sign *n; 
} 
       itoa      把一整数转换为字符串 
例程序: 
#include <ctype.h> 
#include <stdio.h> 
void      itoa (int n,char s[]); 
//atoi 函数:将s转换为整形数 
int main(void ) 
{   
int n; 
char s[100]; 
printf("Input n:/n"); 
scanf("%d",&n); 
        printf("the string : /n"); 
        itoa (n,s); 
return 0; 
} 
void itoa (int n,char s[]) 
{ 
int i,j,sign; 
if((sign=n)<0)//记录符号 
      n=-n;//使n成为正数 
        i=0; 
do{ 
      s[i++]=n%10+'0';//取下一个数字 
}while ((n/=10)>0);//删除该数字 
if(sign<0) 
      s[i++]='-'; 
s[i]='/0'; 
for(j=i;j>=0;j--)//生成的数字是逆序的,所以要逆序输出 
      printf("%c",s[j]); 
}  


是int 转string类型的一个函数 
msdn上是这么写的 
_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow 
Convert an integer to a string. 

char *_itoa( int value, char *string, int radix ); 

char *_i64toa( __int64 value, char *string, int radix ); 

char * _ui64toa( unsigned _int64 value, char *string, int radix ); 

wchar_t * _itow( int value, wchar_t *string, int radix ); 

wchar_t * _i64tow( __int64 value, wchar_t *string, int radix ); 

wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix ); 

Routine Required Header Compatibility 
_itoa <stdlib.h> Win 95, Win NT 
_i64toa <stdlib.h> Win 95, Win NT 
_ui64toa <stdlib.h> Win 95, Win NT 
_itow <stdlib.h> Win 95, Win NT 
_i64tow <stdlib.h> Win 95, Win NT 
_ui64tow <stdlib.h> Win 95, Win NT 


For additional compatibility information, see Compatibility in the Introduction. 

Libraries 

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value 

Each of these functions returns a pointer to string. There is no error return. 

Parameters 

value 

Number to be converted 

string 

String result 

radix 

Base of value; must be in the range 2 – 36 

Remarks 

The _itoa, _i64toa, and _ui64toa function convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 bytes) in string. If radix equals 10 and value is negative, the first character of the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa respectively. 

Generic-Text Routine Mappings 

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_itot _itoa _itoa _itow 


Example 

/* ITOA.C: This program converts integers of various 
* sizes to strings in various radixes. 
*/ 

#include <stdlib.h> 
#include <stdio.h> 

void main( void ) 
{ 
char buffer[20]; 
int i = 3445; 
long l = -344115L; 
unsigned long ul = 1234567890UL; 

_itoa( i, buffer, 10 ); 
printf( "String of integer %d (radix 10): %s/n", i, buffer ); 
_itoa( i, buffer, 16 ); 
printf( "String of integer %d (radix 16): 0x%s/n", i, buffer ); 
_itoa( i, buffer, 2 ); 
printf( "String of integer %d (radix 2): %s/n", i, buffer ); 

_ltoa( l, buffer, 16 ); 
printf( "String of long int %ld (radix 16): 0x%s/n", l, 
buffer ); 

_ultoa( ul, buffer, 16 ); 
printf( "String of unsigned long %lu (radix 16): 0x%s/n", ul, 
buffer ); 
} 


Output 

String of integer 3445 (radix 10): 3445 
String of integer 3445 (radix 16): 0xd75 
String of integer 3445 (radix 2): 110101110101 
String of long int -344115 (radix 16): 0xfffabfcd 
String of unsigned long 1234567890 (radix 16): 0x499602d2 


Data Conversion Routines 

See Also _ltoa, _ultoa

### C语言中 `atoi` 和 `itoa` 函数的定义、区别与使用方法 #### 定义 - **`atoi` 函数**用于将字符串转换成整数。该函数接收一个指向字符串的指针作为参数,并返回对应的整数值[^1]。 ```c int atoi(const char *str); ``` - **`itoa` 函数**则相反,它负责把整数转换为字符串表示形式。需要注意的是此函数并非C标准库的一部分,在某些编译器环境下可能不可用或需自定义实现[^4]。 ```c char* itoa(int value, char* string, int radix); ``` 这里 `value` 是待转换的整数;`string` 存储结果的位置;而 `radix` 表示目标基数(比如2代表二进制,8八进制,10十进制等)[^5]。 #### 使用实例 当需要执行简单的字符串到整数或是反过来的操作时,这两个函数非常方便: 对于 `atoi()` 来说, ```c #include <stdio.h> #include <stdlib.h> int main(){ const char* str = "123"; int num = atoi(str); // 将字符串"123"转化为整数123 printf("Converted number is %d\n",num); return 0; } ``` 而对于 `itoa()` ,由于不是所有环境都支持这个非标准化版本,则建议查看具体平台文档确认可用性或者考虑采用替代方案如`sprintf` 或者手动编写类似的逻辑来完成同样的任务[^2]。 ```c #ifdef _MSC_VER // 如果是在MSVC环境中 #include <cstdlib> // ...其他代码... char buffer[32]; itoa(-42,buffer,10); // 把-42按照十进制存入buffer数组中形成"-42" #else // 对于不提供itoa的情况下的处理方式 snprintf(buffer,sizeof(buffer),"%d",-42); #endif ``` #### 主要差异 两个函数的主要不同在于它们的作用方向上——一个是解析给定字符序列并尝试解释其作为一个有效的整数表达式[^3],另一个则是构建一个新的字符序列用来描述指定的整数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值