We help IT Professionals succeed at work.

How do I create a binary number code?

nelayagvb
nelayagvb asked
on
379 Views
Last Modified: 2010-04-02
I can't figure out how to write a binary code where the user enters a 10 digit binary number and I convert it to the decimal equivalent.
Comment
Watch Question

It is quite hard to code directly in machine language. I would rather try using a compiler, C or C++, or an interpreter (Perl) .
Are you familiar with / using a compiler ?

Author

Commented:
I'm familiar with C++. I don't understand what exactly I'm supposed to be writing down.  How in the world do I type to let the computer know to convert a binary to decimal?

Author

Commented:
I know I'm supposed to do something with powers. I guess I don't know if I'm supposed to use an if statement or if/else or whatever and how I'm suppsed to set it up. I know how to do cout and cin stuff.
Well let say you have a string (I dont know which C++ you use, so I keep C as the language)

char *text = "32449823";

So the text variable containts characters. E.g. text[0] is not the number 3, but it is the ascii character code 51 (decimal).

What you want is to get the number corresponding to this text. The library contains a few functions which do that conversion, e.g.

long num;
num = atol (text);      // and you get the number 32449823 in num.

A long on a PC compiled with VC++ or with gcc is made of 32 bits. So the larger number (unsigned) is 2^32-1 and signed: -2^31 <-> 2^31-1
which is 10 digits (about +/-2,000,000,000  or 4,000,000,000 if unsigned).

Now if the question is that you want to handle the conversion by yourself, an easy algorithm like

unsigned long convert(char *s)
{
    unsigned long x = 0;

    while (*s) x = x * 10 + *s++ - '0';

    return x;
}



From what I understand the Q was to convert from binary into decimal. But it seems to be homework therefor experts are not allowed to give solutions but only guidance. Or show us your code and we can tell you where problems arise therein.

A solution could be using the capabilities of sscanf. Look in the help how to specify a binary input. Not sure whether this way is allowed for you.
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Homework solved.
Hi,

This program converts decimal to binary but it is huge...
...and can take numbers upto 35 million

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <string>

using namespace std;

int main()
{
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//Alphabets representing places
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, o = 0, p = 0, q = 0, r = 0, s = 0, t = 0, u = 0, v = 0, w = 0, x = 0, y = 0, z = 0;


//Input variable definitions
int userinput = 0, input = 0, rem = 0;
char yesno;
//int result;

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
cout << "This program converts numbers from decimal to binary system" << endl << "(Made by Ramkumar)" << endl << endl;

start:
cout << endl << "Enter a number less than 35 million: ";
cin >> userinput;
input = userinput;

calc:
if (input>=33554432){
z=1;
rem=input-33554432;
goto loop;
}
if (input>=16777216){
y=1;
rem=input-16777216;
goto loop;
}
if (input>=8388608){
x=1;
rem=input-8388608;
goto loop;
}
if (input>=4194304){
w=1;
rem=input-4194304;
goto loop;
}
if (input>=2097152){
v=1;
rem=input-2097152;
goto loop;
}
if (input>=1048576){
u=1;
rem=input-1048576;
goto loop;
}
if (input>=524288){
t=1;
rem=input-524288;
goto loop;
}
if (input>=262144){
s=1;
rem=input-262144;
goto loop;
}
if (input>=131072){
r=1;
rem=input-13072;
goto loop;
}
if (input>=65536){
q=1;
rem=input-65536;
goto loop;
}

//32768 and lower
if (input>=32768){
p=1;
rem=input-32768;
goto loop;
}
if (input>=16384){
o=1;
rem=input-16384;
goto loop;
}
if (input>=8192){
n=1;
rem=input-8192;
goto loop;
}

//4096 and lower
if (input>=4096){
m=1;
rem=input-4096;
goto loop;
}
if (input>=2048){
l=1;
rem=input-2048;
goto loop;
}
if (input>=1024){
k=1;
rem=input-1024;
goto loop;
}

//512 and lower
if (input>=512){
j=1;
rem=input-512;
goto loop;
}
if (input>=256){
i=1;
rem=input-256;
goto loop;
}
if (input>=128){
h=1;
rem=input-128;
goto loop;
}
if (input>=64){
g=1;
rem=input-64;
goto loop;
}
if (input>=32){
f=1;
rem=input-32;
goto loop;
}

//16 and lower
if (input>=16){
e=1;
rem=input-16;
goto loop;
}
if (input>=8){
d=1;
rem=input-8;
goto loop;
}
if (input>=4){
c=1;
rem=input-4;
goto loop;
}
if (input>=2){
b=1;
rem=input-2;
goto loop;
}
if (input>=1){
a=1;
rem=input-1;
goto loop;
}

//0; rem=0
loop:
if(rem==0){
goto print;
}
input = rem;
//cout << rem << endl;
goto calc;

print:

cout << endl << userinput << " = " << z << y << x << w << v << u << t << s << r << q << p << o << n << m << l << k << j << i << h << g << f << e << d << c << b << a << endl << endl;

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

cout << "Would you like to convert another number?(y|n) ";
cin >> yesno;
if (yesno == 'y' || yesno == 'Y'){
//reset all

//Alphabets representing places
a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, o = 0, p = 0, q = 0, r = 0, s = 0, t = 0, u = 0, v = 0, w = 0, x = 0, y = 0, z = 0;

//Input variable definitions
userinput = 0, input = 0, rem = 0;
char yesno;

//*reset complete

goto start;

}

system ("PAUSE");
return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

If you cannot build a program that does the reverse, please tell me and I will post one.

Regards,

Ram

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.