繁体   English   中英

C 中的八进制或十六进制数

[英]octal or hexadecimal number to decimal in C

扫描一个数字及其基值。 如果一个数字以 0 开头,则为八进制;如果以 0x 开头,则为十六进制。 打印相应的十进制值。

#include<stdio.h>
int main()
{
int a,b;
scanf("%x %d",&a,&b);
printf("%d",a);`
}

3个测试用例显示错误。请帮助我。

使用 scanfi 标志:

匹配可选签名的 integer; 下一个指针必须是指向 int 的指针。 如果 integer 以 0x 或 0X 开头,则在 base 16 中读取,如果以 0 开头,则在 base 8 中读取,否则在 base 10 中读取。 仅使用与基础相对应的字符。

#include<stdio.h>

int main(void)
{
    int a,b;
    scanf("%i %i",&a,&b);
    printf("%d %d\n", a, b);`
    return 0;
}
#include<stdio.h>
int main(void)
{
    int oc,hex;
    scanf("%i",&oc);
    scanf("%i",&hex);
    printf("\ndecimal:%d",oc);
    printf("\ndecimal:%d",hex);
    return 0;
}

另一种方式

#include<stdio.h>
int main()
{
    int oc,hex;
    printf("enter the octal value:");
    scanf("%o",&oc);
    printf("enter the hex value:");
    scanf("%x",&hex);
    printf("\noctal to decimal:%d",oc);
    printf("\nhex to decimal:%d",hex);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM