C/C++_atoi,itoa功能及其实现原理

itoa将数字转换成指定进制的字符串

itoa是广泛应用的非标准C语言扩展函数。由于它不是标准C语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在<stdlib.h>头文件中包含这个函数。

char *itoa( int  value, char *string, int  radix);
int value 被转换的 整数,char *string 转换后储存的 字符 数组,int radix 转换进制数,如2,8,10,16 进制等
itoa操作使用 itoa操作使用
头文件: <stdlib.h>程序例:
1
2
3
4
5
6
7
8
9
10
#include <stdlib.h>
#include <stdio.h>
int  main( void )
{
int  number=123456;
char  string[25];
itoa(number,string,10);
printf ( "integer=%d string=%s\n" ,number,string);
return0;
}
/* 实现itoa函数的 源代码 */
itoa流程图 itoa流程图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
char * itoa(intnum, char *str,intradix)
{ /*索引表*/
char  index[]= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
unsigned unum; /*中间变量*/
int  i=0,j,k;
/*确定unum的值*/
if (radix==10&&num<0) /*十进制负数*/
{
unum=(unsigned)-num;
str[i++]= '-' ;
}
else  unum=(unsigned)num; /*其他情况*/
/*转换*/
do {
str[i++]=index[unum%(unsigned)radix];
unum/=radix;
} while (unum);
str[i]= '\0' ;
/*逆序*/
if (str[0]== '-' )k=1; /*十进制负数*/
elsek=0;
char  temp;
for (j=k;j<=(i-1)/2;j++)
{
temp=str[j];
str[j]=str[i-1+k-j];
str[i-1+k-j]=temp;
}
return  str;
}
C语言程序 C语言程序
itoa的第三个参数用于将数字转换成不同的进制。举个例子:
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdlib.h>
#include<stdio.h>
intmain( void )
{
intnumber=12345;
charstring[25];
itoa(number,string,10); //按十进制转换
printf ( "integer=%dstring=%s\n" ,number,string);
itoa(number,string,16); //按16进制转换
printf ( "integer=%dstring=%s\n" ,number,string);
return0;
}
输出结果:
1
2
integer=12345string=12345--说明12345的十进制表示就是12345
integer=12345string=3039——说明12345的十六进制表示是0x3039
但是要注意,itoa并不是一个标准的C函数,它是Windows特有的,如果要写跨平台的程序,请用sprintf。
用几进制表示吧:)
MSDN的例子
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*ITOA.C:Thisprogramconvertsintegersofvarious
*sizestostringsinvariousradixes.
*/
#include<stdlib.h>
#include<stdio.h>
voidmain( void )
{
charbuffer[20];
inti=3445;
longl=-344115L;
unsignedlongul=1234567890UL;
_itoa(i,buffer,10);
printf ( "Stringofinteger%d(radix10):%s\n" ,i,buffer);
_itoa(i,buffer,16);
printf ( "Stringofinteger%d(radix16):0x%s\n" ,i,buffer);
_itoa(i,buffer,2);
printf ( "Stringofinteger%d(radix2):%s\n" ,i,buffer);
_ltoa(l,buffer,16);
printf ( "Stringoflongint%ld(radix16):0x%s\n" ,l,buffer);
_ultoa(ul,buffer,16);
printf ( "Stringofunsignedlong%lu(radix16):0x%s\n" ,ul,buffer);
}
1
2
3
4
5
6
Output
Stringofinteger3445(radix10):3445
Stringofinteger3445(radix16):0xd75
Stringofinteger3445(radix2):110101110101
Stringoflongint-344115(radix16):0xfffabfcd
Stringofunsignedlong1234567890(radix16):0x499602d2
指定要转换的进制的基数,其值好象在1--36之间都可以


此外当然可以使用Windows自带的转换函数,sprintf将数字转换为字符串

这个不是C标准库中的函数,而是Windows平台下扩展的,标准库中有sprintf,功能比这个更强,用法跟printf类似:
char str[255];
sprintf(str, "%x", 100); //将100转为16进制表示的字符串。



atoi函数


atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数

atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中

C语言库函数名

atoi

原型:

int atoi(const char *nptr);

UNICODE

_wtoi()

2函数说明编辑

参数nptr字符串,如果 第一个非空格字符存在,是数字或者 正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回 整型数。否则,返回零。
包含在头文件 stdlib.h

3例子:编辑

(1)
1
2
3
4
5
6
7
8
9
10
11
#include <stdlib.h>
#include <stdio.h>
 
int  main( void )
{
     int  n;
     char  *str =  "12345.67" ;
     n =  atoi (str);
     printf ( "int=%d\n" ,n);
     return0;
}
输出:
int = 12345
(2)
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdlib.h>
#include <stdio.h>
 
int  main()
{
     char  a[] =  "-100" ;
     char  b[] =  "123" ;
     int  c;
     c =  atoi (a) +  atoi (b);
     printf ( "c=%d\n" , c);
     return  0;
}
执行结果:
c = 23


  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值