Friday, December 31, 2010

Sample code, convert hex to bin ENJOY.








import javax.swing.JOptionPane;

class J{
static String[] hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
static String[] binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
 static String input1, input2;
static String result1 ,result2;
public static void main(String[] arguments) {

input1 = JOptionPane.showInputDialog(null, "ist digit");
input2 = JOptionPane.showInputDialog(null, "Sseond digit x");

result1=fun1();
result2=fun2();
JOptionPane.showMessageDialog(null, "Hex to Bin = " + fun1() + fun2());
System.exit(0);
}

 static String fun1(){
 for(int i=0; i < input1.length(); i++)//LOOP USER INPUT
 {

char tempa=input1.charAt(i); //decl var temp to hold uiScan   for casting string ;              charAt(i) handles every char
String temp1=""+tempa+"";    //tempObj cast string to hex 
for(int j=0;j<hex.length;j++)  // loop global hex EQUIVALENTS with the converted strin
    {
   if(temp1.equalsIgnoreCase(hex[j])) //nice body
    {
    result1=binary[j];               //result into bin[get j ]
    }
  }
 }
return result1 ;
}

 static String fun2(){
 for(int i=0; i < input2.length(); i++)//LOOP USER INPUT
 {
 
  char tempb=input2.charAt(i  ); //
charAt(i) handles every 
  String temp2=""+tempb+"";     
  for(int j=0;j<hex.length;j++)// loop global hex EQUIVALENTS with the converted strin
    {
   if(temp2.equalsIgnoreCase(hex[j])) //nice body
    {
    result2=binary[j];          //result into bin[get j ]
    }
  }
 }
return result2 ;
}
}



No comments: