char * 转成 int

如 char*p="1234567" ;

#include <stdlib.h> 

int atoi(const char *nptr); 

long atol(const char *nptr); 

long long atoll(const char *nptr); 

long long atoq(const char *nptr);

扩展资料:

反之int 转 char *

在stdlib.h中有个函数itoa()

itoa(i,num,10); 

i     需要转换成字符的数字  

num   转换后保存字符的变量 

10    转换数字的基数(进制)10就是说按照10进制转换数字。

还可以是2,8,16等等你喜欢的进制类型 

原形:char *itoa(int value, char* string, int radix);

实例: 

#include "stdlib.h" #include "stdio.h" main() 

int i=1234; 

char s[5]; 

itoa(i,s,10); 

printf("%s",s); 

getchar(); 

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-06-19
这里主要是atoi和atof的用法
#include "stdio.h"
#include "stdlib.h"
main()
{
char *p="1234567";
int x;
x=atoi(p);
printf("%d\n",x);
}

若果楼主写的是char*p="1234.567"
则是 x=atof(p);

C语言库函数名: atoi
功 能: 把字符串转换成整型数.
名字来源:array to integer 的缩写.
函数说明:
atoi()会扫描参数nptr字符串,如果第一个字符不是数字也不是正负号返回零,否则开始做类型转换,之后检测到非数字或结束符 \0 时停止转换,返回整型数。
原型: int atoi(const char *nptr);
需要用到的头文件: #include <stdlib.h>

而atof是将字串转换成浮点型数本回答被提问者采纳
第2个回答  2020-05-11
孤傲。
12-07
10:15:08□:
09:52:54那是因为一个整数占4个字节,一个char只占一个字节,当int转char时,会选择int的低四位来付给char,而现在257在内存中为10000000
00000000
00000001
00000001把最后的00000001付给了char所以就是1了啊
第3个回答  2010-06-19
main()
{
char *p="123456";
int x;
x=atoi(p);
printf("%d\n",x);
}
这样就可以了,我通过调试了,x就是转换以后的int,直接用这个函数就可以
第4个回答  2019-06-21
首先循环将字符串内替换成数字,例如
for(int
i=0;i<strlen(buf);i++)
{
switch(buf[i])
{
case
'A':
……
break;
}
}
然后atoi(字符串)
相似回答