aspose file tools*
The moose likes Beginning Java and the fly likes How to convert binary to decimal? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert binary to decimal? " Watch "How to convert binary to decimal? " New topic
Author

How to convert binary to decimal?

Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Hi all.... I am new in Java Programming and need your help for the following :

Convert binary numbers to decimal.
The program should prompt user to enter the number. Upon clicking the button, the program should display the number and the decimal equivalent of the number.


Please help. I really appreciate it.

Thanks.
[ February 09, 2005: Message edited by: Alisha Burke ]
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,

The binary conversion utilizes an exponent, so you could use a method pow() from Math class in java.lang [Math.pow()] or make up your own logic.

Make sure you understand the conversion

If you read the input as a string and place the string in a array the position number of the array of the array will be reversed unless you place logic to change it.

ie.

Program...

So you must make sure you grasp the binary logic for conversion and then create some programming logic to complete you lesson.

[ January 23, 2005: Message edited by: Jimmy Die ]

[ edited to break long code lines -ds ]
[ January 23, 2005: Message edited by: Dirk Schreckmann ]

Jimmy Die
Parth Sagdeo
Ranch Hand

Joined: Mar 18, 2004
Posts: 40
Although I didn't fully read the previous post, it seems reasonable to
1)Read the line into a String
2)Starting at the end of the string, start summing the exponents: i.e.
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Thank u both. Though i don't really understand some parts of the codes (as java ain't my forte)....i am trying it out right now.

I'll post again when i finish compiling.
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
I know how to convert binary to decimal...octal to decimal ... and vice versa as I have learned that prior taking this java programming class.

And I have no difficulty creating buttons n labels....that's the easiest part.

What I don't know is how to use the pow method for the conversion part as I am not gonna have any fix number....user will type lets say...01001 or 111000 .... so how do i capture the numbers from right to left n convert it to decimal?

I noe the conversion is like :

(0*16) + (1*8) + (0*4) + (0*2) +( 0*1)

and I tried using ifElse statements...but i think there's a much simpler n easier way to do....well...maybe i give u what i've done n can shed on some light on where i went wrong...

i m such a dummy in this i need to be knock on the head...


well here goes:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Numberpro extends Applet implements ActionListener
{

Label first, one, two;
TextField satu, dua;
Button con;

int answer;//number that is entered in the textfield

double a1,a2,a4,a8,a16;//declare the 5 digit binary to decimal conversion power
//example:2^0=1 , 2^1=2 , 2^2=4 , 2^3=8 , 2^4=16

int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,za,zb,zc,zd,ze;
//declare if 00000 , 00001 , 00010 then it becomes.....



String type="binary";//convert binary base to decimal
String type2="octal";//convert octal base to decimal
String type3="";


public void init()
{first=new Label ("Number System");
add(first);

one=new Label ("Enter the number : ");
add(one);
satu=new TextField(9);
add(satu);
satu.addActionListener(this);

two=new Label ("Enter the base system ");
add(two);
dua=new TextField(9);
add(dua);
dua.addActionListener(this);

con=new Button ("Convert");
add(con);
con.addActionListener(this);}


public void actionPerformed(ActionEvent event)
{answer=Integer.parseInt(event.getActionCommand());
repaint();

type3=dua.getText();
repaint();}

public static double pow(int a, int b)
{a1 = Math.pow(2.0, 0.0);
a2 = Math.pow(2.0, 1.0);
a4 = Math.pow(2.0, 2.0);
a8 = Math.pow(2.0, 3.0);
a16 = Math.pow(2.0, 4.0);}


public void paint(Graphics g)

{a=a1;
b=a2;
c=(a2+a1);
d=a4;
e=a4+a1;
f=a4+a2;
g=a4+a2+a1;
h=a8;
i=a8+a1;
j=a8+a2;
k=a8+a2+a1;
l=a8+a4;
m=a8+a4+a1;
n=a8+a4+a2;
o=a8+a4+a2+a1;
p=a16;
q=a16+a1;
r=a16+a2;
s=a16+a2+a1;
t=a16+a4;
u=a16+a4+a1;
v=a16+a4+a2;
w=a16+a4+a2+a1;
x=a16+a8;
y=a16+a8+a1;
z=a16+a8+a2;
za=a16+a8+a2+a1;
zb=a16+a8+a4;
zc=a16+a8+a4+a1;
zd=a16+a8+a4+a2;
ze=a16+a8+a4+a2+a1;}

if (type.equals(type3))
{g.drawString("answer " , 40, 55 );}


if (type=00000)
{g.drawString("Decimal Equivalent " + 0 , 60, 75 );}
else
if (type=00001)
{g.drawString("Decimal Equivalent " + a , 60, 75 );}
else
if (type=00010)
{g.drawString("Decimal Equivalent " + b , 60, 75 );}
else
if (type=00011)
{g.drawString("Decimal Equivalent " + c , 60, 75 );}
else
if (type=00100)
{g.drawString("Decimal Equivalent " + d , 60, 75 );}
else
if (type=00101)
{g.drawString("Decimal Equivalent " + e , 60, 75 );}
else
if (type=00110)
{g.drawString("Decimal Equivalent " + f , 60, 75 );}
else
if (type=00111)
{g.drawString("Decimal Equivalent " + g , 60, 75 );}
else
if (type=01000)
{g.drawString("Decimal Equivalent " + h , 60, 75 );}
else
if (type=01001)
{g.drawString("Decimal Equivalent " + i , 60, 75 );}
else
if (type=01010)
{g.drawString("Decimal Equivalent " + j , 60, 75 );}
else
if (type=01011)
{g.drawString("Decimal Equivalent " + k , 60, 75 );}
else
if (type=01100)
{g.drawString("Decimal Equivalent " + l , 60, 75 );}
else
if (type=01101)
{g.drawString("Decimal Equivalent " + m , 60, 75 );}
else
if (type=01110)
{g.drawString("Decimal Equivalent " + n , 60, 75 );}
else
if (type=01111)
{g.drawString("Decimal Equivalent " + o , 60, 75 );}
else
if (type=10000)
{g.drawString("Decimal Equivalent " + p , 60, 75 );}
else
if (type=10001)
{g.drawString("Decimal Equivalent " + q , 60, 75 );}
else
if (type=10010)
{g.drawString("Decimal Equivalent " + r , 60, 75 );}
else
if (type=10011)
{g.drawString("Decimal Equivalent " + s , 60, 75 );}
else
if (type=10100)
{g.drawString("Decimal Equivalent " + t , 60, 75 );}
else
if (type=10101)
{g.drawString("Decimal Equivalent " + u , 60, 75 );}
else
if (type=10110)
{g.drawString("Decimal Equivalent " + v , 60, 75 );}
else
if (type=10111)
{g.drawString("Decimal Equivalent " + w , 60, 75 );}
else
if (type=11000)
{g.drawString("Decimal Equivalent " + x , 60, 75 );}
else
if (type=11001)
{g.drawString("Decimal Equivalent " + y , 60, 75 );}
else
if (type=11010)
{g.drawString("Decimal Equivalent " + z , 60, 75 );}
else
if (type=11011)
{g.drawString("Decimal Equivalent " + za , 60, 75 );}
else
if (type=11100)
{g.drawString("Decimal Equivalent " + zb , 60, 75 );}
else
if (type=11101)
{g.drawString("Decimal Equivalent " + zc , 60, 75 );}
else
if (type=11110)
{g.drawString("Decimal Equivalent " + zd , 60, 75 );}
else
if (type=11111)
{g.drawString("Decimal Equivalent " + ze , 60, 75 );}


}


I have another question for you too:

1) For the TextField can i use ActionListener instead of TextListener....will it still work?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
To convert a number with an arbitrary number of digits, you probably should use a loop of some kind. Since your input is textual, you can access individual characters in the String with String.charAt(). If you do something along these lines, it will greatly reduce the number of lines of code. It will also make your code more flexible as you can convert numbers with any number of digits, rather than just the numbers with up to 5 binary digits, as you have it now.

HTH

Layne


Java API Documentation
The Java Tutorial
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Correct me if I'm wrong but can't you just do:

Integer integer = Integer.valueOf(inputString, 2);
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Steven Bell:
Correct me if I'm wrong but can't you just do:

Integer integer = Integer.valueOf(inputString, 2);


Sure, that is possible. However, this looks like a homework assignment. So converting the number manually is probably required by the teacher.

Layne
[ January 23, 2005: Message edited by: Layne Lund ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
miss mushroom,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Thanks for giving me some clues....

Actually my assignment is to convert binary/octal base to decimal.
But I'm doing one step at a time....

I'll post again after i finish coding.

*wink*
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I'd like to point out that your name still doesn't seem to comply with the Naming Policy. You should read the link that Dirk gave above. It states that your display name should be "a name that at least looks like it could belong to a real person."

Layne
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Hi All...


Do you know any good books that deals with conversion in general and calculations in java.

I have borrowed 4 so far but none giving me the examples n tutorials that I want.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Hi, Alishaa

Welcome to JavaRanch! Please adjust your display name to meet the JavaRanch Naming Policy.

We want you to be able to continue to post freely here at JavaRanch.

You can change it here.

Thanks!

Marilyn


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Hi Marilyn...

Thanx 4 giving me the 'warning'....changed my name already....

hope this one is ok
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Alishaa:
Hi All...


Do you know any good books that deals with conversion in general and calculations in java.

I have borrowed 4 so far but none giving me the examples n tutorials that I want.


If you know a bit of programming and a bit of mathematics you should be able to apply examples in just about any language to any other language you know, and be able to create your own code based on textual descriptions of algorithms.


42
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Hi everyone...

I have made some changes on my code. I am doing the binary to decimal conversion (left out the octal to decimal conversion----will do that later)

However after compiling there r 2 errors :
[illegal start of type
identifier expected]
>>>>>Did i missed something??<<<<


Here's my code.... all comments...good/bad r welcome. I got a month to finish this assignment so there r no worries 4 me..



[/CODE]

[ January 25, 2005: Message edited by: Alisha Burke ]
[ January 26, 2005: Message edited by: Alisha Burke ]
Alisha Burke
Greenhorn

Joined: Jan 22, 2005
Posts: 27
Just to inform u all that i have manage to compile my code.
Only i don't get the answer to the conversion of the binary number that
i m looking 4. Still need to work on it. Thanks all for helping me .
[ January 26, 2005: Message edited by: Alisha Burke ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert binary to decimal?