Thread: Converting base 10 to base 2

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    15

    Converting base 10 to base 2

    Hey guys. I am doing an extra credit assignment where I need to convert any number from 0 to 18,446,744,073,709,551,615. I got down the basic layout to get base 10 into base 2, but backwards. Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int a,b;
    
        printf("BASE 10 TO BASE 2 CONVERTER\n\n");
        printf("Enter the base 10 number you want to convert:");
        scanf("%d", &a);
    
        while(a!=0)
        {
            b=a%2;
            a=a/2;
            printf("%d", b);
        }
        return 0;
    }
    So I have 2 questions.

    1. How can I make "printf("%d", b);" to make it actually become a whole number instead of printing each number one by one? Would I use an array? If not an array, how would I reverse the numbers?

    2. Which data type can I use to receive such a large input? Would I use an unsigned float maybe?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Storing the result in an array, then printing the array in reverse is one way.

    Using recursion to print a/2 before printing a%2 is another way.

    > 2. Which data type can I use to receive such a large input? Would I use an unsigned float maybe?
    Your max number is a full 64-bit number.
    You could try 'unsigned long long int a' with scanf("%ull",&a);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    15
    Quote Originally Posted by Salem View Post
    Storing the result in an array, then printing the array in reverse is one way.

    Using recursion to print a/2 before printing a%2 is another way.

    > 2. Which data type can I use to receive such a large input? Would I use an unsigned float maybe?
    Your max number is a full 64-bit number.
    You could try 'unsigned long long int a' with scanf("%ull",&a);
    How do I assign a value to a certain c[i] array? Also how do would I know how much to assign to the array when declaring it?

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    15
    So I got the program all set up and it works. But only if I use int. Whenever I used unsigned int or unsigned long long int, the program constantly spits out 0 or 1 on to the screen nonstop. Here is what I have so far:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int a, b, d, c[36]={0}, i;
        i=0;
    
    
        printf("BASE 10 TO BASE 2 CONVERTER\n\n");
        printf("Enter the base 10 number:");
        scanf("%d", &a);
        d=a;
    
    
        while (a!=0)
        {
            b=a%2;
            a=a/2;
            c[i]=b;
            i++;
        }
    
    
        printf("\"%d\" in base 10 is equal to \"", d);
        for(i=i-1; i>=0; i--)
            printf("%d", c[i]);
        printf("\" in base 2\n");
    
    
    
    
        return 0;
    }
    The problem I also run into is creating an array using unsigned long long int, then trying to set b, an unsigned long long int, to c[i], which is an unsigned int or whatever.

    Also what does recursive mean? I never learned about that.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you made your loop variable unsigned, then it would loop forever, because >= 0 would always be true.

    Code:
    #include <stdio.h>
     
    int main()
    {
        unsigned int a, b, d, c[36]={0};
        int i;
        i=0;
     
     
        printf("BASE 10 TO BASE 2 CONVERTER\n\n");
        printf("Enter the base 10 number:");
        scanf("%u", &a);
        d=a;
     
     
        while (a!=0)
        {
            b=a%2;
            a=a/2;
            c[i]=b;
            i++;
        }
     
     
        printf("\"%d\" in base 10 is equal to \"", d);
        for(i=i-1; i>=0; i--)
            printf("%d", c[i]);
        printf("\" in base 2\n");
     
     
     
     
        return 0;
    }
    
    
    $ ./a.out 
    BASE 10 TO BASE 2 CONVERTER
    
    Enter the base 10 number:255
    "255" in base 10 is equal to "11111111" in base 2
    $ ./a.out 
    BASE 10 TO BASE 2 CONVERTER
    
    Enter the base 10 number:65536
    "65536" in base 10 is equal to "10000000000000000" in base 2
    > Also what does recursive mean? I never learned about that.
    For pizza, there's mastercard
    For everything else, there's google.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling base class function when base is a template
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2010, 02:26 AM
  2. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  3. Converting Base 10 numbers to Base 2, 8, and 16
    By killcapital in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 10:05 AM
  4. converting to base 10
    By xp5 in forum C Programming
    Replies: 11
    Last Post: 08-31-2007, 02:28 AM
  5. Replies: 9
    Last Post: 10-07-2006, 05:37 AM

Tags for this Thread