Converting Base 10 numbers to Base 2, 8, and 16

This is a discussion on Converting Base 10 numbers to Base 2, 8, and 16 within the C Programming forums, part of the General Programming Boards category; I'm having trouble writing this program that converts base 10 (decimal) numbers to base 2(binary), base 8(octal), and base 16(hexadecimal). ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Converting Base 10 numbers to Base 2, 8, and 16

    I'm having trouble writing this program that converts base 10 (decimal) numbers to base 2(binary), base 8(octal), and base 16(hexadecimal). They conver to binary and octal using placeholder notation, however when it comes to converting to base 16/hexadecimal, the program does not read the assigned chars. if you're not familiar with hexadecimal, any number after 9 is assigned a letter (i.e. 10=A, 11=B, 12=C, etc.) it actually converts to hexadecimal, but it just doesn't convert it respectively to the letters after 9.

    here's where i'm at:

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
          int a,
              b,
              c,
              d=-1,
              choice;
          char const A=10,
                     B=11,
                     C=12,
                     D=13,
                     E=14,
                     F=15,
                     G=16;
    
          char ans;
    
          for(;;)
          {
    
           printf("Type in any integer to be converted into another base: ");
           scanf("&#37;d",&a);
           printf("Select another base representation (2 or 8 or 16). \n\n2 represents "
                  "binary notation\n\n8 represents octal notation\n\n16 represents hexadecimal notation");
           scanf("%d",&choice);
    
           switch (choice)
           {
            case 2:
                 b=a;
                 while (b!=0)
                 {
                  c=b%2;
                  b/=2;
                  ++d;
                  printf("%d x 2^%d\n",c,d);
                 }
            break;
            {
             case 8:
                  b=a;
                  while (b!=0)
                  {
                   c=b%8;
                   b/=8;
                   ++d;
                   printf("%d x 8^%d\n",c,d);
                  }
            break;
            {
             case 16:
                  b=a;
                  while (b!=0)
                  {
                   c=b%16;
                   b/=16;
                   ++d;
                   printf("\n%d x 16^%d\n",c,d);
                  }
            }
            }
           }
    
          printf("Do you wish to run again? (Y/N)");
          ans=getchar();
          ans=getchar();
    
          if (ans=='N')
             break;
    
          }
          printf("\n");
         
          system("PAUSE");
          return 0;
    }
    Last edited by killcapital; 03-08-2008 at 12:12 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way:
    Code:
    if (c < 10)
    {
                   printf("\n%d x 16^%d\n",c,d);
    }
    else
    {
                   printf("\n%c x 16^%d\n",'A'+(c-10), d);
    }

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    G=16; there is no such digit
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    16
    hiiiii this can print any base 10 number to any other base upto 16-base..
    Code:
    #include<stdio.h>
    static unsigned long int base;
    int main(void)
    { unsigned long int num;
    printf("Enter the Decimal:");scanf("&#37;ld",&num);
    printf("\nNumber and converting Base :");scanf("%ld",&base);
    printf("Value of %ld with base of %ld is:",num,base);
    convert(num);
    getch();
    }
    convert(unsigned long int num)
    { if(num){convert(num/base);printf("%c","0123456789ABCDEF"[num%base]);}
    }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by krishna View Post
    hiiiii this can print any base 10 number to any other base upto 16-base..
    You have a horrible indentetion and horrible style

    Code:
    #include<stdio.h>
    static unsigned long int base; /* do not use globals */
    int main(void)
    { unsigned long int num;
    printf("Enter the Decimal:");scanf("%ld",&num); /* do not place two statements on the same line */
    printf("\nNumber and converting Base :");scanf("%ld",&base);
    printf("Value of %ld with base of %ld is:",num,base);
    convert(num);
    getch(); /* this is not standard - and also missing include file - should be removed */
    }
    convert(unsigned long int num) /* missing return type */
    { if(num){convert(num/base);printf("%c","0123456789ABCDEF"[num%base]);} /* why not to place all you program on same line - it will be even less readable than now */
    }
    [/QUOTE]
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    I would suggest using an array in this problem since your printing the numbers backwards because when you convert to a different base the first number is always the least significant digit.

    For example:

    Converting 10 to base 2,

    2/10 --> Q: 5 R: 0
    2/5 --> Q: 2 R: 1
    2/2 --> Q: 1 R: 0
    2/1 --> Q: 0 R: 1

    Q = quotient R = remainder

    Using the printf statement printf("&#37;d x 2^%d\n",c,d); is correct, however it's still not the best way to read the value since you have to read the number upside down. Try a reverse for loop.

    Oh and the statement b = a in each of the switch cases isn't needed since b has scope throughout the entire main function

    This is just my two cents.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21