July 15th, 2004, 07:50 PM
converting decimal to binary in C
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
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?
int a, b, ask;
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 :");
printf("Do you want a leading \"0x\" for the output?");
printf("\nyes = 1, no = 2 : ");
printf("\nThe value %d has the hexadecimal value of 0x%x\n\n",ask,ask);
printf("\nThe value %d has the hexadecimal value of %x\n\n",ask,ask);
printf("\nThis is not a valid option\n");
printf("\nThe value %d has the octal value of %o\n\n",ask,ask);
printf("\nThe value %d has the character value of %c\n\n",ask,ask);
printf("\nThis is not a valid option\n");
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....
July 15th, 2004, 11:44 PM
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)
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.
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:
// reverse them and you have your number:
1010 = 10 (in decimal)
I believe there is a c library that can do it for you, but it's more fun this way :P
I changed the example to make it more readable, putting the moduluses and divisions into two separate columns[/edit]
July 16th, 2004, 06:51 AM
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
July 16th, 2004, 11:39 AM
thanks, i haven't thought about the mod before, i'll give it a try, at least now i know where to look for
July 16th, 2004, 11:57 AM
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.
July 16th, 2004, 07:50 PM
ok, i get the point, but the next problem would be displaying the hex, since it contains numbers and characters....
July 16th, 2004, 09:52 PM
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<10 and letters corresponding to the number if x>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.
July 17th, 2004, 08:15 AM
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.
July 17th, 2004, 09:02 AM
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???
July 17th, 2004, 11:35 PM
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.
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]