C int到二进制

时间:2018-03-28 12:01:10

标签: c raspberry-pi

我正在制作交流程序,必须稍后在树莓派上运行,程序从命令行获取一个参数,确定它的值是否介于0和15之间。如果是,则将其转换为二进制并返回它。 / p>

如果没有,只打印错误信息。

这是代码:

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

int convert(int dec)
{
    if (dec == 0)
    {
        return 0;
    }
    else
    {
        return (dec % 2 + 10 * convert(dec / 2));
    }
}

int main(int argc, char *argv[]) 
{
    // argc is number of arguments given including a.out in command line
    // argv is a list of string containing command line arguments

    printf("the number is:%s \n", argv[2]);

    int v = atoi(argv[2]);
    int bin = 0;

    if(v >= 0 && v <= 15){
        printf("Correct input");
        bin = convert(v);
        printf("Binary is: %s \n", convert(v));
    }
    else{
        printf("Inorrect input, number cant be accepted");
    }
}        

当我使用$./a.out 1在线编译here时,我收到以下错误:

  

号码是:1
  分段错误

输入:

./a.out 12

预期产出:

the number is:12
Correct inputBinary is: 1100

我在stackoverflow上找到的转换方法。

***编辑我的新答案:

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

int convert(int dec)
{
    if (dec == 0)
    {
        printf("Base c\n");
        return 0;
    }
    else
    {
        printf("Rec call\n");
        return ((dec % 2) + 10 * convert(dec / 2));
    }
}

int main(int argc, char *argv[]) 
{
    // argc is number of arguments given including a.out in command line
    // argv is a list of string containing command line arguments

    int v = atoi(argv[2]);
    printf("the number is:%d \n", v);


    int bin = 0;

    if(v >= 0 && v <= 15){
        printf("Correct input \n");
        bin = convert(v);
        printf("Binary is: %d \n", bin);
    }
    else{
        printf("Inorrect input, number cant be accepted");
    }
}     

输出:

the number is:3                                                                                                                    
Correct input                                                                                                                      
Rec call                                                                                                                           
Rec call                                                                                                                           
Base c                                                                                                                             
Binary is: 11   

二进制文件如何必须为0011.这部分是我现在正在处理的部分。

2 个答案:

答案 0 :(得分:5)

您的格式说明符错误:

printf("Binary is: %s \n", convert(v));

convert会返回int,但%s需要char*

你需要:

printf("Binary is: %d \n", convert(v));
                // ^----

传递给该计划的第一个参数是argv[1],而不是argv[2]

因此:

printf("the number is:%s \n", argv[1]);
                                // ^----

int v = atoi(argv[1]);
               // ^----

答案 1 :(得分:0)

这是我的问题的答案:

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

int convert(int dec)
{
    if (dec == 0)
    {
        printf("Base c\n");
        return 0;
    }
    else
    {
        printf("Rec call\n");
        return ((dec % 2) + 10 * convert(dec / 2));
    }
}

int main(int argc, char *argv[]) 
{
    // argc is number of arguments given including a.out in command line
    // argv is a list of string containing command line arguments

    int v = atoi(argv[2]);
    printf("the number is:%d \n", v);


    int bin = 0;

    if(v >= 0 && v <= 15){
        printf("Correct input \n");
        bin = convert(v);
        printf("Binary is: %04d \n", bin);
    }
    else{
        printf("Inorrect input, number cant be accepted");
    }
}                                                       

输入:$./a.out 3 输出:

the number is:3                                                                                                                    
Correct input                                                                                                                      
Rec call                                                                                                                           
Rec call                                                                                                                           
Base c                                                                                                                             
Binary is: 0011