| | |
Binary (base 2) to Decimal (base 10) HELP!
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
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
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.
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
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?

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",•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
unsigned int value would be your decimal value.
The
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.
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.
>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.
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.
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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]
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]
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 0
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
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.
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
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
![]() |
Similar Threads
- counting lesson(for the slow among us) (Posting Games)
- i dont know how to continue... (C++)
- Weird Binary to Integer Problem (Java)
- Urgent Help needed with Pascal programming (Pascal and Delphi)
- adding numbers of binary output (C)
- Binary Fraction conversion question (C)
- hop over decimal and straight to business (C++)
- i need code for binary to decimal (it should be written in 'C') (C)
Other Threads in the C Forum
- Previous Thread: Null Char
- Next Thread: Generate 1 to n mumbers.
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






