学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
 16 12
发新话题
打印

以字符方式读取的数字怎样转化为整形?!

以字符方式读取的数字怎样转化为整形?!

#include <stdio.h>
void main(void)
{
    char p[10]={0};
    printf("请输入数字:");
    scanf("%s",p);
    ...
}

TOP

atoi函数可以把字符型数字转换为整形。
sscanf也可以,而且功能更丰富,可以转16进制的
大家一起来编程吧!

TOP

你不是到中级会员了吗?
还灌?
我看不懂你在说什么啊!
我秀我自己

TOP

回复 3# 的帖子

给个小例子!
谢谢
我秀我自己

TOP

第一种用“atoi”的:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

void main()
{
    char str[1024] = {0};
    int Interger;
    printf("输入一个数字:\n");
    scanf("%s", str);

    Interger = atoi(str);

    printf("转换为整形:%d\n", Interger);
    getch();
}
大家一起来编程吧!

TOP

第二种:
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http://yzfy.org **
*****************************************************************/
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

void main()
{
    char str[1024] = {0};
    int Interger;
    printf("输入一个数字:\n");
    scanf("%s", str);

    sscanf(str, "%d", &Interger);

    printf("转换为整形:%d\n", Interger);
    getch();
}
大家一起来编程吧!

TOP

第二种变种:16进制字符转整形的
引用:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

void main()
{
    char str[1024] = {0};
    int Interger;
    printf("输入一个16进制数字:\n");
    scanf("%s", str);

    sscanf(str, "0x%X", &Interger);    //转16进制

   
printf("转换为整形:%d\n", Interger);
    getch();
}
本帖最近评分记录
  • qinxinhai +4 很好,很精彩! 2008-5-14 19:06
大家一起来编程吧!

TOP

小题目,要是遇到什么耗时间,费脑力的我可没这么好喽
先写自己的程序再说。
大家一起来编程吧!

TOP

谢谢!
非常感谢啊!
我秀我自己

TOP

不知道了,以前做的arm程序有专门的 函数

TOP

 16 12
发新话题