Decimal to binary conversion

This is a discussion on Decimal to binary conversion within the C Programming forums, part of the General Programming Boards category; Ive been trying for days to figure out how to do decimal to binary conversion... ive been trying to modules ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Decimal to binary conversion

    Ive been trying for days to figure out how to do decimal to binary conversion... ive been trying to modules the number by 2 to get the remainder but my proble is saving the remainders in the right order to be printed... the asignment is as follows, print a table of the binary octal and hexadecimal equivalents from 1 to 256. ive got the octal and hexidecimal down... this is basic C programming.. im only in my 7th week of learning it, can someone please help?? here is the code i have already
    Code:
    #include <stdio.h>
    
    int main()
    {
    
      int x;
    
      for( x = 1; x <= 256; x++ ) {
    
        printf( "%d", x );
        printf( "\t" );
        printf( "%o", x );
        printf( "\t" );
        printf( "%x", x );
        printf( "\n" );
      }
    
     return 0;
    }
    Thanks for your help!!
    Antoni

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    hope this bit helps in your case

    Code:
    #include<stdio.h>
    
    int main()
    {
            int num,i=0;
            int binary[10]={0};
    
            scanf("%d",&num);
            printf("Binary value of %d is ",num);
            while(num!=0)
            {
                
                binary[i++]=num%2;
                num/=2;
        }
        
        for(;i>=0;i--)
        printf("%d",binary[i]);
         
            getchar();
      }
    this code calculates the binary value of the decimal value entered by the user

    s.s.harish

  3. #3
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    You could do this too,


    Code:
    #include <stdio.h>
    #define MASK 0x00000001
    
    int main()
    {
        int x, i;
       for(x =1; x <= 256; x++)
         {
            printf( "%d\t", x );
            printf( "%o\t", x );
            printf( "%x\t", x );
    
            for(i = 0; i < 33; i++)
              {
                 printf("%i", (x && MASK));
                 x = x >> 1;
              }
             printf("\n");
         }
      return 0;
    }
    Its bitwise operators which is not basic so if you do not want to use it thats ok.

  4. #4
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    Quote Originally Posted by Dave_Sinkula
    >You could do this too

    I don't suppose you tried this code first?
    Oh boy I am sorry, definately should have done that. I fixed it here.

    Code:
    #include <stdio.h>
    #define MASK 0x80000000
    
    int main()
    {
        int x, t, i;
       for(x =1; x <= 256; x++)
         {
            printf( "%d\t", x );
            printf( "%o\t", x );
            printf( "%x\t", x );
            t = x;
            for(i = 0; i < 33; i++)
              {
                 printf("%i", (t && MASK));
                 t = t << 1;
              }
             printf("\n");
         }
      return 0;
    }
    Edited: Realized it in class it's not a good day.
    Last edited by stumpster123; 04-07-2005 at 01:46 PM.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Stumpster123: The way you print out the binary is still wrong since it goes in the wrong direction. So 2 is meant to be 0000 0010 but your code will print it as 0100 0000. A simple fix to this is to place the values into an array and print out the array in the reverse order once you have all the bits down.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A simpler way is to start with the high bit and work down to the low bit.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-04-2008, 01:39 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Decimal to binary conversion help needed
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 02-06-2002, 01:03 PM

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