c++ binary to decimal conversion..
i'm trying to get this binary to decimal convertor working..
could someone help me a tad on this.. Code:
int argc, count, intconvert, total=0; |
Binary to decimal:
Code:
#include<iostream> |
trying to limit myself by using c instead of c++..
anybody else..? |
Might I suggest a loop to calculate the decimal value. Also.. could you do all your error checking in one shot in a loop then calculate the decimal value rather than error check each bit as you go along?? It could have saved you a lot of typing.
ex. Code:
for (int i = 0; i < NUM_LENGTH; i++) Code:
for (int i = 0; i < NUM_LENGTH; i++) Good Luck |
okay.. i've looked into what you mentioned and it seems the pow function is kind of new to me.. but after reading some of it in the book, i understand that 'i' increments and 2 is raised to the power of the incremented 'i'..
but i'm not sure what you mean by "NUM_LENGTH" in both examples.. i'm guessing that those two are variables that i have to declare or the input where i would somehow get the length by using strlen(). and last.. you say "calculating the number".. i'm not sure but shouldn't i divide the input.. for ex. input is 100101011.. i would have to divide intconvertbin (the input) by using Code:
intconvertbin[i]*pow(2,i) + number sorry.. a tad just got to a nab.. |
Sorry 'bout that... that was just a quick cookie cutter example to give you another idea on where to go
NUM_LENGTH is the size of your intconvertbin[] array. It seems like yours is 16 or 17. You probably declared the array size somewhere in main(). Quote:
LSB: starting at right :) int number = 0; number = intconvertbin[0 or i] * 2^(0 or i) + number so number = 1*2^0 + 0 => number = 1 Bit position 1: number = intconvertbin[1 or i]*2^(1 or i) + number so number = 1*2^1 + 1 => number = 3 Bit position 2: number = intconvertbin[2 or i]*2^(2 or i) + number so number = 0*2^2 + 3 => number = 3 and so on... Sorry 'bout the confusion. I think that should work. There is probably a much easier solution. Gtk usually sees them. |
maybe i'm looking at it wrong myself.. but thanks anyways.. gonna try this tomorrow and see if i can mess around with it..
that one part about the pow() function looks useful.. thanks.. and good night.. |
okay.. here's my whole code..
Code:
/************************* Code:
else if(inputbase1=='b') anybody can give a hint.. |
What kind of output (or errors) are you getting?
By the way, to avoid some redundant calculation, you can eliminate the pow() function, and just update a variable each time through the loop: Code:
int curPow = 1; The printf is outside the loop, since presumably you just want the answer at the end, rather than the number in progress (before it is done being calculated). Also, I haven't looked too closely, but I presume that intconvertbin[] contains bits in ascending order of significance? i.e., that: intconvertbin = {0, 1, 0, 1} is equal to 0 ones, 1 twos, 0 fours, and 1 eight (a total of 10). |
alright.. i'll try that after i finish some other studies i'm doing..
helpful by the looks of your code.. |
Man.. I'm sorry for suggesting that.. I'm thinking about a fix. I think I just made more trouble for you. Gimme a couple minutes.
|
Ok.. I ran your code and this is what I think is happening... so far
scanf("%c", &intconvertbin); getchar(); This is placing the char you read in to all spaces in intconvertbin Here is a quick fix to place it in each spot. scanf("%16c", intconvertbin); Now if you printf each char you will see whatever you input instead of all 1's or all 0's. Now you can run atoi on each index and use wapcaplet's elegant power solution. |
okay.. i put wapcaplet's code in and tested it.. but i didn't put in rmartine's "%16c" in the scanf and added the getchar() function.. with the "%16c", the scanf() function will ask about 3 or 4 times input from the user before moving on to the next thing.. so.......
the code i'm using right now is: Code:
else if(inputbase1=='b') the output i'm getting from that code is: Quote:
the code looks like it should work.. but it just seems there's only minor bugs in it.. any takes..? |
Why are you using both scanf() and getchar()? I'd think that would grab too many digits and ignore the second one.
Without just telling you how to do it, I'd suggest putting in some additional output. Maybe you could focus first on making sure that the digits are read in correctly. Try reading them in one at a time, and printing out the digit you read. After you make sure that works, then you can add in the conversion code. Try tracing through it on paper, too. Follow the code through the loop, and write down the values of each variable, and the stuff that would be printed out. That always helps me understand the problem a little better, and helps to find bugs too! |
All times are GMT -5. The time now is 03:16 PM. |