/*
Here is a java-based code which converts a hexadecimal, binary and octal to decimal equivalent,
corrections and suggestions are highly appreciated.
*/
package converter;
import java.io.*;
import java.lang.*;
import java.util.Scanner;
public class converter{
public static void main(String[] args){
int integer_input=0;
int base_input=0;
int choice;
String again="y";
Scanner input=new Scanner(System.in);
BufferedReader again_input = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.print("\n");
System.out.println("Input as\n");
System.out.println("1. Binary \n2. Octal \n3. Hexadecimal \n4. QUIT");
System.out.print("\nEnter choice: ");
choice=input.nextInt();
switch(choice){
case 1:{
System.out.print("\n");
String myBinary="";
BufferedReader binary_input = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("Input binary digits(ex.1001110): ");
myBinary=binary_input.readLine();
for(int i=0;i
System.out.println("Sorry Invalid input");
System.exit(1);
}
}
int len=myBinary.length();
int storeBinaryInput[] = new int[len];
for(int j=0; j
storeBinaryInput[j]=Character.getNumericValue(myBinary.charAt(j));
}
BinaryToDecimal(storeBinaryInput,len);
}
catch(Exception io){}
break;
}
case 2:{
System.out.print("\n");
String myOctal="";
BufferedReader octal_input = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("Input octal digits(ex.112): ");
myOctal=octal_input.readLine();
int len=myOctal.length();
int storeOctalInput[] = new int[len];
for(int j=0; j
storeOctalInput[j]=Character.getNumericValue(myOctal.charAt(j));
}
OctalToDecimal(storeOctalInput,len);
}
catch(Exception io){}
break;
}
case 3:{
System.out.print("\n");
String myHexa="";
BufferedReader hexa_input = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Use letters A-F only");
System.out.print("Input hex digits(ex.589 or 8F): ");
myHexa=hexa_input.readLine();
int len=myHexa.length();
int storeHexaInput[] = new int[len];
for(int j=0; j
storeHexaInput[j]=Character.getNumericValue(myHexa.charAt(j));
}
HexaToDecimal(storeHexaInput,len);
}
catch(Exception io){}
break;
}
case 4:{
System.out.println("Bye!");
System.exit(1);
break;
}
default:{
System.out.println("Invalid Input");
}
System.out.print("Do you still want to continue [y][n] ");
try{
again=again_input.readLine();
}
catch(Exception io){
}
}
}while(again.charAt(0)=='Y' || again.charAt(0)=='y');
}
public static void BinaryToDecimal(int storedBinaryHere[],int len){
int result=0;
int j=len-1;
for(int i=0;i
result+=storedBinaryHere[i]*Math.pow(2,j);
j--;
}
System.out.println("Decimal equivalent is "+result+" base 10");
}
public static void OctalToDecimal(int storedOctalHere[],int len){
int result=0;
int j=len-1;
for(int i=0;i
result+=storedOctalHere[i]*Math.pow(8,j);
j--;
}
System.out.println("Decimal equivalent is "+result+" base 10");
}
public static void HexaToDecimal(int storedHexaHere[],int len){
int result=0;
int j=len-1;
for(int i=0;i
result+=storedHexaHere[i]*Math.pow(16,j);
j--;
}
System.out.println("Decimal equivalent is "+result+" base 10");
}
}
Tuesday, September 16, 2008
Convertion (hexadecimal, binary and octal to decimal equivalent)
Posted by
Carlo Carollo
at
12:43 PM
Labels: Programming
Subscribe to:
Post Comments (Atom)
Search
Categories
- Extra Income (1)
- Funny (2)
- Just a Thought (6)
- Outings (1)
- Programming (9)
- Santos (1)
- Thesis/Project Ideas (1)
- Troubleshooting (4)
- Ultimate Fighting Championship (3)
- Web Programming (7)
Featured Video
News Bloopers!!!

0 comments:
Post a Comment