//package arpClasses.Drawing;

import java.math.*;

public class numberConverter{
	public double convertToDouble(String input){
		double value;
		char sign = input.charAt(0);
		String exp = input.substring(1,12);
		int e = BintoDec(exp)-1023;
		
		String sif = input.substring(12);
		value = Math.pow(2,e);
		value = value * (1+BintoDec2(sif));
		//System.out.println("val ="+value);
		return value;
	}
   public int BintoDec(String binString){
    	 int value=0;
       try{
       	int len = binString.length();
       	for(int i=0; i<len;i++){
       		
       		char c = binString.charAt(i);
       		if(c=='0'){
       			value = value + (int)Math.pow(2,len-1-i)*0;
       		}
       		else{
       			value = value + (int)Math.pow(2,len-1-i)*1;
       		}
       		
       	}//end for loop
       	return value;	
       }catch(Exception e){
       	System.out.println(e);
       	return -1;
       }
    }//end BintoDec
    public double BintoDec2(String binString){
    	 double value=0;
       try{
       	int len = binString.length();
       	for(int i=0; i<len;i++){
       		
       		char c = binString.charAt(i);
       		if(c=='0'){
       			value = value + Math.pow(2,-(i+1))*0;
       		}
       		else{
       			value = value + Math.pow(2,-(i+1))*1;
       		}
       		
       	}//end for loop
       	return value;	
       }catch(Exception e){
       	System.out.println(e);
       	return -1.0;
       }
    }//end BintoDec2	

    
    //method convert a hex string to a dec Integer
    public static Integer toDecInteger(String hexString){
    	 int value=0;
       try{
       	int len = hexString.length();
       	for(int i=0; i<len;i++){
       		
       		char c = hexString.charAt(i);
       		if(c=='0'){
       			value = value + (int)Math.pow(16,len-1-i)*0;
       		}
       		else if(c=='1'){
       			value = value + (int)Math.pow(16,len-1-i)*1;
       		}
       		else if(c=='2'){
       			value = value + (int)Math.pow(16,len-1-i)*2;
       		}
       		else if(c=='3'){
       			value = value + (int)Math.pow(16,len-1-i)*3;
       		}
       		else if(c=='4'){
       			value = value + (int)Math.pow(16,len-1-i)*4;
       		}
       		else if(c=='5'){
       			value = value + (int)Math.pow(16,len-1-i)*5;
       		}
       		else if(c=='6'){
       			value = value + (int)Math.pow(16,len-1-i)*6;
       		}
       		else if(c=='7'){
       			value = value + (int)Math.pow(16,len-1-i)*7;
       		}
       		else if(c=='8'){
       			value = value + (int)Math.pow(16,len-1-i)*8;
       		}
       		else if(c=='9'){
       			value = value + (int)Math.pow(16,len-1-i)*9;
       		}
       		else if(c=='a'||c=='A'){
       			value = value + (int)Math.pow(16,len-1-i)*10;
       		}
       		else if(c=='b'||c=='B'){
       			value = value + (int)Math.pow(16,len-1-i)*11;
       		}
       		else if(c=='c'||c=='C'){
       			value = value + (int)Math.pow(16,len-1-i)*12;
       		}
       		else if(c=='d'||c=='D'){
       			value = value + (int)Math.pow(16,len-1-i)*13;
       		}
       		else if(c=='e'||c=='D'){
       			value = value + (int)Math.pow(16,len-1-i)*14;
       		}
       		else if(c=='f'||c=='F'){
       			value = value + (int)Math.pow(16,len-1-i)*15;
       		}
       		else{
       			return null;
       		}
       	}//end for loop
       	return new Integer(value);	
       }catch(Exception e){
       	return null;
       }
       	
    }
    public String toBinaryString(int value){
    	 String binString = Integer.toBinaryString(value);
    	 while(binString.length()<8){
    	 	binString='0'+binString;
    	 }
    	 //System.out.println(binString);
    	 return binString;
    }
}