atoi 和 itoa 自身用法和背后实现的过程
atoi顾名思义就是ascll转化成int
itoa就是将转int化成ascll
ok,
atoi自身用法:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
char sh[]="123d4";
int ch = atoi(sh);
printf("%d\n",ch);
return 0;
}
背后;#include<stdio.h>
void my_atoi(int m,char str[])
{
int i=0;
m = 0;
while(str[i] != 0)
{
if(str[i]>='0'&& str[i] <='9')
{
m = str[i]-'0'+m *10;
}
else
{
str[i] = 0;
break;
}
i ++;
}
printf("%d",m);
}
int main ()
{
char sh[]="12312";
int x = 0;
my_atoi(x,sh);
return 0;
}
itoa的用法
#include<stdio.h>
#include<stdlib.h>
int main()
{
char s[20];
int h =48;
printf("%s\n",itoa(h,s,2));
return 0;
}
背后实现过程:</pre><pre code_snippet_id="1740230" snippet_file_name="blog_20160701_7_4225828" name="code" class="html">#include<stdio.h>
void my_itoa(char st[],int a,int m)
{
int i = a;
int j = 0;
while(a >0)
{
if (m >= 2 && m <=10)
{
st[j] = a % m +'0';
a = a /m;
}
else if(m > 10 && m <= 36)
{
if((a % m +'0') >= '0'&& (a % m +'0') <= '9')
{
st[j] = a % m +'0';
a = a /m;
}
else
{
st[j] = a % m +'0'+7;
a = a /m;
}
}
i --;
j ++;
}
st[j]=0;
strrev(st);
}
int main()
{
char s[20];
int h = 2000;
my_itoa(s,h,36);
printf("%s\n",s);
return 0;
}
结束了,希望这些代码可以帮助大家在进制转化方面有所帮助!