How to convert Binary to Decimal?

Aug 5, 2008 at 3:47am
Hi guyz!

I know how to convert decimal to binary but I've found that converting
binary to decimal is much much harder. Maybe my knowledge
about the codes is limited to most most basic.

Please show me a program that converts binary to decimal.
I'll study it.
Please put some remarks for the codes purpose.

Thanks!
Aug 5, 2008 at 6:45am
it is not difficoult.
you have multiply each binary numeral by a power of 2 and add them all together:
1110= 0*2^0+1*2^1+1*2^2+1*2^3=14

100=0*2^0+0*2^1+1*2^2=4

...
Aug 5, 2008 at 12:23pm
Do you have an Array of characters (zeros and ones) or do have some bits and you want to make a decimal output?
Aug 5, 2008 at 6:17pm
There are a number of links on this page that may help.
http://mathforum.org/dr.math/faq/faq.bases.html

Frankly, converting between radices is so elementary that we can't just give you the code for it. You have to understand what is going on underneath and then you'll write the code yourself without breaking a sweat.

Remember, numbers don't have radix. Only representations of numbers (the stuff we humans like to read, or the physical representation of a number) has radix.

Base 2 (binary) means two digits per power. 0 and 1.
0000 <-- 0
0001 <-- 1
0010 <-- 2 : this is essential the "ten" of binary. Hence the old joke:
"there are only 10 kinds of people: those who understand binary and those who don't".

Base 8 (octal) means eight digits per power: 0, 1, 2, 3, 4, 5, 6, and 7.
Base 10 (decimal) means ten digits per power: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Base 16 (hexadecimal) means 16 digits per power: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

The difference is in the power.
123 in decimal is 1×102 + 2×101 + 3×100 123 in octal is 1× 82 + 2× 81 + 3× 80
etc.

Hope this helps.
Last edited on Aug 5, 2008 at 6:19pm
Aug 8, 2008 at 1:07am
What I mean is if I input 11110011.
How can I get the 1st char in from left then the 2nd then 3rd then ... to test if it is 1 or 0?

I don't like to input 1 enter then input 1 again in the next line then 0 in the next ...

FYI, I master binary to octal to hexa but I'm a newbie in c++.
This program is easy to make in qbasic, VB because the codes are easy to understand.
I'm having a difficulty in c++ because of sensitive codings.
Aug 8, 2008 at 1:17am
You can read the input into an array or a string. And then access each character individualy.

Here is a small example using a string to get you started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main(){
   string bin;
   cout << "Give binary code: ";
   cin >> bin;
   for(int i=0; i<bin.size(); i++){
      cout << bin[i] << "  ";//You can access each character in a string like an array
                             //Not the best way but a good way to start
   }
   return 0;
}
Last edited on Aug 8, 2008 at 1:17am
Topic archived. No new replies allowed.