Sunday, September 4, 2011

Simple Java program to convert Binary numbers to Decimal ones

Despite the simplicity of the code, it took me a couple of hours to workaround some characteristics of of the Java language that were not for me, they were playing against me. Never had I struggled too much to simply convert characters to integers, my goodness!!! That was supposed to be a piece of cake, but after lots of different tries, and frustrating ones since that was supposed to be too easy to be true, I eventually circumvented the problem using arrays of types Integer, Character and String. Enough, I don't want to remember that again, so let's go to the code:

===============================================


package com.silvestri.bin.to.dec;

import javax.swing.JOptionPane;

public class DecimalConverter {

public static void main(String[] args) {
String binaryNumber;
int numberOfCharacters;
int maximumPower;
binaryNumber = JOptionPane.showInputDialog(null, "What's the Binary number to be converted? ");
 
for (int i = 0; i < binaryNumber.length(); i++) {
if (binaryNumber.charAt(i) != '0' && binaryNumber.charAt(i) != '1') {
JOptionPane.showMessageDialog(null, "You haven't input a binary number");
  return;
}
}
 
numberOfCharacters = binaryNumber.length();
maximumPower = numberOfCharacters - 1;
Character[] charArray = new Character[numberOfCharacters];
String[] stringArray = new String[numberOfCharacters];
Integer[] intArray = new Integer[numberOfCharacters];
 
Integer[] poweredDigits = new Integer[numberOfCharacters];
 
for (int i = 0; i < numberOfCharacters; i++) {
charArray[i] = binaryNumber.charAt(i);
stringArray[i] = charArray[i].toString();
intArray[i] = Integer.parseInt(stringArray[i]);

}

for (int i = 0; i < intArray.length; i++, maximumPower--)
poweredDigits[i] = (int)(Math.pow(2, maximumPower)) * ((intArray[i]));
 
int sum = 0;
for (int i = 0; i < intArray.length; i++)
sum += poweredDigits[i];

JOptionPane.showMessageDialog(null, "The equivalent Decimal number is: " + sum);

}

}

===============================================

Some may come and tell "there's a much simpler way to convert that," such as in the example below:

-------------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class BinarytoDecimal {

    public static void main(String[] args) throws Exception {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the binary value:");
         String s = br.readLine();
        System.out.println("Decimal value is : "+Integer.parseInt(s, 2));

    }
}

found at: http://www.roseindia.net/java/java-get-example/java-binary-decimal.shtml

-------------------------------------------------------------------------------------

To those ones I tell that I wanted to convert the number myself, not ask for the Java Virtual Machine to do it for me. I was also considering writing that problem in C in order not to count on some of the facilities of the Java language, who knows I go for it at another opportunity...

I'm running out of time now, lots of things to do, I hope you appreciate the code, see you later!!!

No comments:

Post a Comment