char[]如何转换int

zyip 2008-09-14 09:20:43
char a[]="12346";
如何将a[]转换为int x= 12345
...全文
366 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
sheshijie 2008-09-22
  • 打赏
  • 举报
回复
12楼写的好!
关键部分 取出值 x-0x30
变为整数 循环*10、100、100....
cxxer 2008-09-22
  • 打赏
  • 举报
回复
char a[]="12346";
如何将a[]转换为int x= 12345

方法1:


#include <stdio.h>

int main()
{
char a[] = "1234";
int x = 0;

char *p = a;

while (p)
{
x = x*10 + (*p);
p++;
}
}


方法2:


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

int main()
{
char a[] = "1234";
int x = atoi(a);
}
  • 打赏
  • 举报
回复
有个函数叫做atoi

a to i

下载个手册看看!~
devil_zuiai 2008-09-22
  • 打赏
  • 举报
回复
楼上自己写的函数对于负数好像没有用吧?
char str[] = "-123";
cout <<atoi(str) << endl;
所以还是用
atoi()或者sscanf()吧。
ysysbaobei 2008-09-17
  • 打赏
  • 举报
回复
mark 学些
tiancai20042008 2008-09-17
  • 打赏
  • 举报
回复
atoi
如果是int 变字符串可以用sprintf 和strcat函数
weidong0210 2008-09-17
  • 打赏
  • 举报
回复
想自己写也很简单
xiansizhe 2008-09-15
  • 打赏
  • 举报
回复
atoi
HelloDan 2008-09-15
  • 打赏
  • 举报
回复
方法多多啊。
atoi()

sprintf()

还有就是用ASCII码个个来减的。
WEN2222 2008-09-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 BEISHUISHILAN 的回复:]
x=atoi(a);


函数名称: atoi

函数原型: int atoi(char *str)

函数功能: 将字符串转换成一个整数值

函数返回: 转换后的数值

函数说明: str 待转换为整型数的字符串

所属文件: <stdlib.h>
[/Quote]
这样的函数自己是可以写的,楼主还是试一下吧
LiTuX 2008-09-15
  • 打赏
  • 举报
回复
stdlib中的atoi,atol等等众多函数可以用;

stdio中有(我最经常用的)sscanf函数等等……

oo_v_oo 2008-09-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 pointerfree 的回复:]
int x= 12345  这个行嘛?
int 最大不就256嘛
[/Quote]
呵呵
challenger_zh 2008-09-15
  • 打赏
  • 举报
回复

int ParseInteger(char *pChar)
{
int len;
len=strlen(pChar);
int value=0;
int t=1;
for(int i=1;i<len;i++)
{
t*=10;
}
for(i=0;i<len;i++)
{
value+=(pChar[i]-'0')*t;
t/=10;
}
return value;
}
visame 2008-09-15
  • 打赏
  • 举报
回复
sscanf
atoi
strtol
aozhi 2008-09-15
  • 打赏
  • 举报
回复
atoi
sscanf都可以
星羽 2008-09-15
  • 打赏
  • 举报
回复
最简单就atoi
tobylee999 2008-09-15
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 pointerfree 的回复:]
int x= 12345 这个行嘛?
int 最大不就256嘛
[/Quote]

难道使用的8位的苹果机?
hmsuccess 2008-09-14
  • 打赏
  • 举报
回复
有点疏漏,不好意思

#include<stdio.h>
#include <malloc.h>
#include <string.h>
#include <memory.h>


int my_trans(const char *);

int main()
{
char i[]="12346";
my_trans(i);
return 0;
}

int my_trans(const char *str)
{
int len = strlen(str);
int *j = (int *)malloc(len*sizeof(int));
if (j==NULL)
{
printf("malloc is bad!!\n");
return -1;
}
memset(j,0,len*sizeof(int));
int result=0 ,index=1;
int k;

for(k=0;k<len;++k)
{
j[k]=str[k]-0x30;
}
for(k=len-1;k>=0;--k)
{
result +=j[k]*index;
index *=10;
}

printf("%d\n",result);
free(j);
return result;
}

hmsuccess 2008-09-14
  • 打赏
  • 举报
回复
sscanf atoi
或者自己处理

#include<stdio.h>
#include <malloc.h>
#include <string.h>
#include <memory.h>


int my_trans(const char *);

int main()
{
char i[]="12346";
my_trans(i);
return 0;
}

int my_trans(const char *str)
{
int len = strlen(str);
int *j = (int *)malloc(len*sizeof(int));
if (j==NULL)
{
printf("malloc is bad!!\n");
return 1;
}
memset(j,0,len*sizeof(int));
int result=0 ,index=1;
int k;

for(k=0;k<len;++k)
{
j[k]=str[k]-0x30;
}
for(k=len-1;k>=0;--k)
{
result +=j[k]*index;
index *=10;
}

printf("%d\n",result);
free(j);
}
BEISHUISHILAN 2008-09-14
  • 打赏
  • 举报
回复
x=atoi(a);


函数名称: atoi

函数原型: int atoi(char *str)

函数功能: 将字符串转换成一个整数值

函数返回: 转换后的数值

函数说明: str 待转换为整型数的字符串

所属文件: <stdlib.h>
加载更多回复(14)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧