Decimal to binary conversion help needed

This is a discussion on Decimal to binary conversion help needed within the C Programming forums, part of the General Programming Boards category; I have to write a program in C that will print a table of decimal, binary, octal, and hexadecimal equivalents. ...

  1. #1
    Unregistered
    Guest

    Question Decimal to binary conversion help needed

    I have to write a program in C that will print a table of decimal, binary, octal, and hexadecimal equivalents. I know how to get the program to print the octal and hex, but I don't know how to get it to convert decimal to binary and print the binary.

    Basically I want it to print a table like this:

    Decimal Octal Hex Binary
    0 0 0 0
    1 1 1 0001


    all the way up to 15.

    Can anyone help?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sounds like homework to me. I'm not doing all of your homework for you. Surely you've learned bitwise manipulation by now?

    if( somevar & 1 ) printf("1"); else printf("0");

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

  3. #3
    Unregistered
    Guest
    Originally posted by quzah
    Sounds like homework to me. I'm not doing all of your homework for you. Surely you've learned bitwise manipulation by now?

    if( somevar & 1 ) printf("1"); else printf("0");

    Quzah.
    No, we haven't learned that. The textbook is lousy and so is the prof. The only thing I know how to do is get the program to display octal and hex.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, have him teach you about the binary operators:

    & | ^ ~

    You should be able to search the forums for the topic you've posted. There have been many posts on it in the past. What you do is compare each variable a bit at a time, and then print out the end result.

    Quzah.
    Last edited by quzah; 02-05-2002 at 07:15 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Well, there is another way if bitwise operators aren't an option. Let's say you want to print the binary number as a string, and you initialize the string to have eight ASCII zeroes (and a NULL zero at the end, of course). Then you could simply do a series of if statements to decide which of the zeroes should be set to one.

    Here's some pseudocode:
    • If the number is greater than 128, subtract 128 from the number and set the first character of our string to ASCII '1'.
      If the number is greater than 64, subtract 64 from the number and set the second character of our string to ASCII '1'.
      If the number is greater than 32, ...


    This could be done with a series of if statements, but if you sit down and think about it you will see there is a way to pull this off with a single for loop. The trick to using the for loop is realizing you will have to keep track of more than one variable in your for statement.

    I hope this gives you just enough help to finish your assignment (without actually giving away the answer).
    Jason Deckard

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Well to extract each digit in turn you do

    x % base - to extract the least significant digit
    x / base - to move the next least significant digit into place

    You keep doing this until x is 0

  7. #7
    Sayeh
    Guest
    shifting can be handled with multiplication or division by 2. You can also do it with addition and subtraction (demonstrated below) if you know what the values of the bits are that you are working with.

    For example:


    Code:
    /* Prototypes */
    
    int main(void);
    
    
    /* Tables */
    
    unsigned byte   bits[] = {128,64,32,16,8,4,2,1};
    char                   alphaBinary[9];
    
    
    /* Functions */
    
    int main(void)
       {
       int                     i;
       unsigned byte  byteBinary;
    
       byteBinary = 113;                             /* this could be any 0-255 value */
    
       for(i=0;i<8;i++)                                 /* loop through value */
          {
          if((byteBinary - bits[i]) < 0)            /* If subtracting highest bit goes negative, then bit is zero */
             alphaBinary[i] = '0';
          else
             {
             alphaBinary[i] = '1';                    /* otherwise it is set */
             byteBinary -= bits[i];                  /* effectively shift right */
             };
          };
    
       alphaBinary[9] = 0x00;                    /* plug in c-style terminating byte */
    
       printf("Original Decimal Value:  %i  Binary Equivalent:  %s.",byteBinary,alphaBinary);
       
       return(0);
       }
    Not tested, but this should give you the right idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting decimal to binary within a I/O program
    By RandomX in forum C Programming
    Replies: 4
    Last Post: 11-26-2006, 08:25 AM
  2. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 08:51 PM
  3. Decimal to Binary Conversion program
    By acidbeat311 in forum C Programming
    Replies: 5
    Last Post: 01-12-2006, 09:26 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 12:06 AM

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