Convert decimal to binary in C

后端 未结 16 691
攒了一身酷
攒了一身酷 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:01

    Here is the Algorithm to convert Decimal to Binary number

    • Divide the input decimal number by 2 and store the remainder.
    • Store the quotient back to the input number variable.
    • Repeat this process till quotient becomes zero.
    • Equivalent binary number will be the remainders in above process in reverse order.

    You can check c program here http://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html

    0 讨论(0)
  • 2020-12-10 10:01
    int main() 
    { 
     int n, c, k;
     printf("Enter an integer in decimal number system: ");
     scanf("%d", &n);
     printf("%d in binary number system is: ", n);
     for (c = n; c > 0; c = c/2) 
      {
       k = c % 2;//To
       k = (k > 0) ? printf("1") : printf("0");
      }
     getch();
     return 0; 
    }
    
    0 讨论(0)
  • 2020-12-10 10:04

    //C Program to convert Decimal to binary using Stack

    #include<stdio.h>
    
    #define max 100
    
    int stack[max],top=-1,i,x;  
    
    
    void push (int x)
    {
      ++top;
      stack [top] = x;
    }
    
    int pop ()
    { 
       return stack[top];
    }   
    
    
    void  main()
    {
      int num, total = 0,item;
      print f( "Please enter a decimal: ");
      scanf("%d",&num);
      while(num > 0)
      {  
        total = num % 2;
        push(total);
        num /= 2;
      }
    
    
    
     for(i=top;top>-1;top--)
     {     
         item = pop ();
         print f("%d",item);
     }
    
     }
    
    0 讨论(0)
  • 2020-12-10 10:05

    A few days ago, I was searching for fast and portable way of doing sprintf("%d", num). Found this implementation at the page itoa with GCC:

    /**
     * C++ version 0.4 char* style "itoa":
     * Written by Lukás Chmela
     * Released under GPLv3.
    
     */
    char* itoa(int value, char* result, int base) {
        // check that the base if valid
        if (base < 2 || base > 36) { *result = '\0'; return result; }
    
        char* ptr = result, *ptr1 = result, tmp_char;
        int tmp_value;
    
        do {
            tmp_value = value;
            value /= base;
            *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
        } while ( value );
    
        // Apply negative sign
        if (tmp_value < 0) *ptr++ = '-';
        *ptr-- = '\0';
        while(ptr1 < ptr) {
            tmp_char = *ptr;
            *ptr--= *ptr1;
            *ptr1++ = tmp_char;
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-10 10:07

    You can do it using while loop under a function also. I was just searching the solve for mine but the solves i get were not suitable, so I have done it accordingly the practical approach (divide using 2 until getting 0 and store the reminder in an array) and print the reverse of the array and Shared Here

    #include <stdio.h>
    
        int main()
        {
            long long int a,c;
            int i=0,count=0;
            char bol[10000];
            scanf("%lld", &a);
            c = a;
            while(a!=0)
            {
                bol[i] = a%2;
                a = a / 2;
                count++;
                i++;
            }
            if(c==0)
            {
                printf("0");
            }
            else
            {
                for(i=count-1; i>=0; i--)
                {
                    printf("%d", bol[i]);
                }
            }
            printf("\n");
            return 0;
        }
    
    0 讨论(0)
  • 2020-12-10 10:13

    First of all 192cannot be represented in 4 bits

    192 = 1100 0000 which required minimum 8 bits.

    Here is a simple C program to convert Decimal number system to Binary number system

    #include <stdio.h>  
    #include <string.h>  
    
    int main()  
    {  
        long decimal, tempDecimal;  
        char binary[65];  
        int index = 0;  
    
        /* 
         * Reads decimal number from user 
         */  
        printf("Enter any decimal value : ");  
        scanf("%ld", &decimal);  
    
        /* Copies decimal value to temp variable */  
        tempDecimal = decimal;  
    
        while(tempDecimal!=0)  
        {  
            /* Finds decimal%2 and adds to the binary value */  
            binary[index] = (tempDecimal % 2) + '0';  
    
            tempDecimal /= 2;  
            index++;  
        }  
        binary[index] = '\0';  
    
        /* Reverse the binary value found */  
        strrev(binary);  
    
        printf("\nDecimal value = %ld\n", decimal);  
        printf("Binary value of decimal = %s", binary);  
    
        return 0;  
    } 
    
    0 讨论(0)
提交回复
热议问题