Binary (base 2) to Decimal (base 10) HELP!

Reply

Join Date: Oct 2008
Posts: 5
Reputation: DogfoodEnforcer is an unknown quantity at this point 
Solved Threads: 0
DogfoodEnforcer DogfoodEnforcer is offline Offline
Newbie Poster

Binary (base 2) to Decimal (base 10) HELP!

 
0
  #1
Oct 3rd, 2008
Hi there Daniweb,

right now im in an engineering program and we are being put through a crash course in C. Ive never dont any type of programming before so it is quite an adventure (to say the least).

Currently we are doing an assignment in which we have to convert a binary number (base 2) to a decimal number (base 10). I think I have the beginnings of the program, but Im not sure how to proceed. Im not sure what variables i should be assigning and how to implement them.

we were given pseudo code to implement into the program, but i think im missing a lot.

Pseudo-code algorithm: (start by convincing yourself that this algorithm works)

1. Prompt the user to enter a binary number, followed by the Enter key

2. Assign variable decimalValue 0

3. Get the first binary digit and Assign variable nextDigit the value of the digit

4. While (there are still digits to be processed)

4.1 Assign variable decimalValue the value of (decimalValue * 2) + nextDigit

4.2 Get the next binary digit and Assign nextDigit the value of the digit

5. Output the value of variable decimalValue

so far i have this:

#include <stdio.h>
#include <stdlib.h>

int decimalValue; 
int binnumber;

int main()
{
   printf("Please enter a binary number: \n");
  
   decimalValue = 0;
 
   scanf("%i", &binnumber);
   
   
  
  system("PAUSE");	
  return 0;
}

should my int binnumber actually be nextDigit (following the pseudo code)?

could someone please help guide me to the next step? i THINK i should be using an 'if' or "while" statement. i know that the program should terminate when the value returned = 0. i also know that in the calculations i need to have something along the lines of "x %10 = a" followed by " a/10 = b" (something along those lines, i used completely random variable letters there). my friend in the class said something about converting an int into a char, but i wasnt sure where he was going with that.lol

with regards to the binary numbers, how many letters should i assign? couldnt someone be difficult and enter a 10 digit binary? in that case, should i have 10 letters (one for each integer) for the binary to decimal function?

i know this must sound absolutely stupid, but this is a very foreign language to me (C, not english.lol) and we have only been taking the course for a few weeks. i cant stand how in the textbook all their examples are so simple! why cant they give harder examples!?lol

i really appreciate any help you can give me. im quite concerned about this as i feel like im only taking in small chunks of what i should be.

ill update the code portion of my post as i proceed. hopefully i dont make a complete **** of myself!

thanks in advance

DFE
Last edited by DogfoodEnforcer; Oct 3rd, 2008 at 5:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #2
Oct 3rd, 2008
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: DogfoodEnforcer is an unknown quantity at this point 
Solved Threads: 0
DogfoodEnforcer DogfoodEnforcer is offline Offline
Newbie Poster

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #3
Oct 3rd, 2008
thank you for that link

in the link, would the "unsigned int value" be my "int decimalValue"?

also, what does this represent? is it necessary? or is it just examples of binary numbers?

#
static const char *binary[] =
#
{
#
"0000","0001","0010","0011","0100","0101","0110","0111",
#
"1000","1001","1010","1011","1100","1101","1110","1111",
#
"1010010110100101", "11011110101011011011111011101111",
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #4
Oct 3rd, 2008
unsigned int value would be your decimal value.

The
static const char *binary
is a list of binary values that are being converted to their decimal equivalent. In your case you are getting this input from the user, so you don't need it.

Also since a user could enter a really long binary value which you will probably not be able to hold in an integer, it makes more sense to store your binary value as a char* as shown in the program.
Last edited by stilllearning; Oct 3rd, 2008 at 6:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #5
Oct 3rd, 2008
>in the link, would the "unsigned int value" be my "int decimalValue"?
Unsigned are positive numbers only.

>also, what does this represent? is it necessary? or is it just examples of binary numbers?
Those are just some example of binary numbers for test purpose. The author implemented them in an array of strings.

[Edit:]The important part for you to understand it is the brief explanation before the example. If you get that, you'll be able to find your own way of doing it.
Last edited by Aia; Oct 3rd, 2008 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: DogfoodEnforcer is an unknown quantity at this point 
Solved Threads: 0
DogfoodEnforcer DogfoodEnforcer is offline Offline
Newbie Poster

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #6
Oct 3rd, 2008
is the const part of the char declaration necessary? i havent seen it utilized before.

so basically this is an extremely easy program, but i just suck at C?lol


in regards to this section of the code. what does size_t i; do? is that just relating to the size of the text? im not 100% sure what it is doing. i see that it declares i as something (as it wasnt used earlier in the program) but i dont know why.

size_t i;

for ( i = 0; i < sizeof binary / sizeof *binary; ++i )

{

unsigned int value = btou(binary[i]);

printf("btou(\"%s\") = 0x%X = %u\n", binary[i], value, value);
Last edited by DogfoodEnforcer; Oct 3rd, 2008 at 8:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #7
Oct 3rd, 2008
The const means that you cannot modify the contents of the char.

In your case, since you are asking the user to input the binary value, so you do not want to make it a const , since you will be modifying it to store your value.

Make sure you allocate enough space to the array though .. like char myBinstring[SIZE]
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: DogfoodEnforcer is an unknown quantity at this point 
Solved Threads: 0
DogfoodEnforcer DogfoodEnforcer is offline Offline
Newbie Poster

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #8
Oct 3rd, 2008
we havent used strings yet in our classwork. are there other ways to approach it?

i think our prof expects us to use modulus functions. such as:

x%10 = y
y/10 = z

then use a loop or something or other to continue through all the binary integers until it ends up being zero.

holy crap. im so bad at debugging.lol

this is going to take a LOOONG time to complete.lol. im trying to use the example as a pseudo-template, but im having zero luck. i think the way that the code writer for the example is doing it isnt the same way that our prof expected (as i said earlier), so i dont want to use these types of functions that we havent even used yet.lol
Last edited by DogfoodEnforcer; Oct 3rd, 2008 at 8:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #9
Oct 3rd, 2008
Modulo will work. If you are using an int to store your binary value make sure that you check the upper limits of the maximum integer value. If you want to be able to store larger values, then you could use a long or a long long.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Binary (base 2) to Decimal (base 10) HELP!

 
0
  #10
Oct 4th, 2008
Doing it a long hard way here is a brief explanation.

If you have a integer input then you first need to split it into indervidual numbers i.e

11010111

would become

1 1 0 1 0 1 1 1

(spaces show a split into an array or whatever)

Then you need to multiply each one by 2^x where x increase by one(starting at 0), however this must be done in reverse order. then you must add them together to give you your decimal number

1x2^7 + 1x2^6 + 0x2^5 + 1x2^4 + 0x2^3 + 1x2^2 + 1x2^1 + 1x2^0
----------------------------------------

As for splitting an integer there are multiple ways, some involve convering it to strings. However going on the idea of normal and modulus division. This is how it would be done

ie the number 11010111
x = (number of chars)

(11010111 % x) / x-1

that would get you the ver far right 1

then next would be
(11010111 % x-1) / x-2
etc

hope this helps
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC