Convert decimal to binary in C

后端 未结 16 773
攒了一身酷
攒了一身酷 2020-12-10 09:38

I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn\'t work:

void de         


        
16条回答
  •  不知归路
    2020-12-10 10:19

    It looks like this, but be careful, you have to reverse the resulting string :-)

    #include 
    #include 
    #include 
    char output[256]="";
    
    int main()
    {
    int x= 192;
    int n;
    n = x;
    int r;
    do {
    r = n % 2;
    if (r == 1)
       strcat(output,"1");
    else strcat(output,"0");
    n = n / 2;
    }
    while (n > 0);
    
    printf("%s\n",output);
    }
    

提交回复
热议问题