Link to home
Start Free TrialLog in
Avatar of nelayagvb
nelayagvb

asked on

How do I create a binary number code?

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.
Avatar of Mercantilum
Mercantilum
Flag of Japan image

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 ?
Avatar of nelayagvb
nelayagvb

ASKER

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?
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.
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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