# Thread: converting decimal to binary in C

1. ## converting decimal to binary in C

hi guys,

i'm learning C at the moment and i'm writing a little program which needs as input a decimal value, and by choice, converts it to octal, hex, or the ascii character.

it would be nice to learn, and to add to the program, the option of converting the decimal value to binary.

but my problem is, i know how to convert decimal to binary by head, but not how to do this if the decimal value is a variable (in this case an integer)

here is what i've got so far, its not that fancy coding, but hey, i'm learning
#include <stdio.h>

int main()
{

printf("\nThis is a number converter.\n");
printf("Enter a decimal value: ");
printf("\nWhich output do you like?\n");
printf("1 = hex, 2 = octal, 3 = character :");
scanf("%d", &a);

switch(a)
{
case 1:
printf("Do you want a leading \"0x\" for the output?");
printf("\nyes = 1, no = 2 : ");
scanf("%d",&b);
switch(b)
{
case 1:
break;
case 2:
break;
default:
printf("\nThis is not a valid option\n");
break;
}
break;
case 2:
break;
case 3:
break;
default:
printf("\nThis is not a valid option\n");
return 0;
}
return 0;
}
let's say i've inputted the value 32, how can make it 0100000 without having to make a table in the program, which looks up the value?

is there an standard option for in C, like %x gives the hex value??

or do i need to write a function?

and if so, how would i do this?

thanks in advance for the replies....

2. If you mod a number by two until you cannot do so anymore you will get the binary equivalent of it.

Modding (or modulusing) a number is where you get the remainder given when you divide it by another number. For example, 2 mod 3 is 2 (zero remainder 2).

If you have the number ten and want to convert it to binary, you do the following:

(% is the c modulus operator)

Code:
```10 % 2 = 0    10 / 2 = 5
5  % 2 = 1    5  / 2 = 2
2  % 2 = 0    2  / 2 = 1
1  % 2 = 1    1  / 2 = 0

// take the answers to the moduluses:

0101

// reverse them and you have your number:

1010 = 10 (in decimal)```
I won't give you c code to add that functionality because it will be more fun to do it yourself. If you get stuck (which I doubt you will), pm me and I'll give you more help.

I believe there is a c library that can do it for you, but it's more fun this way :P

ac

I changed the example to make it more readable, putting the moduluses and divisions into two separate columns[/edit]

3. I agree with gothic_type on this one, if you like code then why not program this on your own? Plus, you can reuse the function later... just make a file and put all the functions you like in it.. then you can include it in your proggies... yup, OO

4. thanks, i haven't thought about the mod before, i'll give it a try, at least now i know where to look for

5. The modulus operator is also handy for other conversions such as hex. Because hex is base 16 (I hope I'm right on that ) you just modulus and divide by 16 instead of by 2. It's a slightly different process to decimal-binary conversion, however.

ac

6. ok, i get the point, but the next problem would be displaying the hex, since it contains numbers and characters....

7. I assume you know of the switch statement? Basically simple hex (as in 0 - 255 I believe) is made up of 2 characters. The first is multiplied by 16, then the second is added. So if you only have two characters available, the highest number is FF which is (15x16)+15 == 255

Basically you have to have some sort of algorithm which displays a number if x&lt;10 and letters corresponding to the number if x&gt;10 (if you can understand that). If you want, I'll post a program that I wrote to convert decimal to hex, but you should have a try first yourself.

If you don't understand what I wrote, try searching google for a table that gives you hex and decimal beside each other and work out the relationship in order to create an algorithm. That's how I made my program.

ac

8. Hmm an exercise that come to mind right now is... to make a function that would accept 3 variables base1, base2, number. The number is in base1 and you need to convert it to base 2. Let's say you can do this only for numeric characters... of course, it's just an idea for an exercise.

9. gothic_type, yes i do know about the switch statement as you can see in above code, and this is surely something worth trying...

hypronix: i don''t understand what you mean, if number is of base1, then why would you need 3 vars as input in the function???

10. lepricaun, you would need three vars because you need to know what base the original number is in (or I suspect you do). The following example should illustrate it for you:

What base is 10 in?

a) Base 2
b) Base 10
c) Base 16

If you can give me the "correct" answer to the above question choosing only a, b, or c, I will give you a pat on the back.

ac

and the comment about whether or not you knew the switch statement was semi-sarcastic (I'm afraid to admit ) I was trying to say "think for yourself"[/edit]

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•