# Thread: Decimal to binary conversion help needed

1. ## 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. 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.

3. 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. 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.

5. 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).

6. 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. 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.