We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,530 Members — Technology Publication meets Social Media

# Decimal to Binary using strings

Hi, Im stuck, Im trying to make a program that asks the user for a decimal number and then it converts it to a binary number. Im assuming I need a "remainder = string % 2" or something along these lines but Im not really sure where to start. anyhelp would be appreaciated.

``````#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{

char string[100];
int s;
char a;
char j;
int sum = 0;

printf("B = B to D\n");
printf("D = D to B\n");
printf("choose which one to convert to:");
scanf("%c%c", &a, &j);

if (a == 'B')
{
printf("enter binary number to convert to decimal: ");
scanf("%s", string);

for(s = strlen(string)-1; s >= 0; s--)
{

if(string[s] == '1')
{
sum = sum + pow(2, strlen(string) - (s +1));

}
}
printf("the decimal number is: %d\n", sum);
}

if (a == 'D')
{
printf("enter decimal number to convert to binary: ");
scanf("%s", string);

}

return 0;
}
``````
3
Contributors
4
Replies
8 Hours
Discussion Span
9 Months Ago
Last Updated
6
Views
ahmed.alsalihi.3
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Skill Endorsements: 0

You just keep going into a loop until the number is equal to 0. Simply append the value from % to the string and then divide the number by 2 (int should not keep the precision anyway). Once you are done with the whole loop, you will get a reversed version of the binary string. Just reverse it back to get the binary.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Skill Endorsements: 17

hmm well I got this far, but when I run it i keep getting 0...

`````` if (a == 'D')
{
printf("enter decimal number to convert to binary: ");
scanf("%s", string);

while (r > 0)
{
r = r%2;
}

printf("the binary number is: %d\n", r);

}

return 0;
}
``````
ahmed.alsalihi.3
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Skill Endorsements: 0

Before line 8, try `cout << (r%2);` and the line 8, you need `r = r/2;` and see what happen... If you enter 8, it should display 0001. Please look at what I wrote in my previous answer again. It contains all steps you need to do...

PS: You could add the new bit in front of the string instead, so you don't need to reverse the string.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Skill Endorsements: 17

I'm confused.

You enter your number into a string named string then start processing a variable named r. Where did r come from? How do you do math on a string?

First thing's first:
Write down the steps you take to translate a decimal value into a binary value -- on paper.
Once you have that written and verified that it works, you convert that into your code.

The problem you (and most new programmers) have is sitting in front of a keyboard with no plan at all and expect code to just fly into the computer. It won't work. Start at your desk and
1) Write down what you need to do. Make a plan
2) Then turn that plan into steps.
3) Break those steps into smaller steps.
4) Verify these steps work as you go.
5) Repeat 3 & 4 until there ain't no more.
6) Last thing is type the code into the computer.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421