java 16進数文字列とdouble型の相互変換

学校の課題で作ったので置いておく
他の言語にも応用できる(無理矢理やったのでもっといい方法はあるきがする)

##16進数文字列からdoubleへの変換
```ruby:MyDouble.java

	//bitのたってるところと何桁目かで値を計算
	private static double bitToDouble(final int bit,final int digit)
	{
		return bit*Math.pow(2, digit);
	}

	//4bitまとめて計算
	private static double bit4ToDouble(final int bit1,final int bit2,final int bit3,final int bit4, final int digit)
	{
		final int bit1Dig=digit*4;
		return bitToDouble(bit1,bit1Dig+3)+bitToDouble(bit2,bit1Dig+2)+bitToDouble(bit3,bit1Dig+1)+bitToDouble(bit4,bit1Dig);
	}
	//16進数文字と桁数から値を計算
	private static double hexCharToDouble(final char c, final int digit)
	{

		switch(c)
		{
		case '0':return bit4ToDouble(0,0,0,0,digit);
		case '1':return bit4ToDouble(0,0,0,1,digit);
		case '2':return bit4ToDouble(0,0,1,0,digit);
		case '3':return bit4ToDouble(0,0,1,1,digit);
		case '4':return bit4ToDouble(0,1,0,0,digit);
		case '5':return bit4ToDouble(0,1,0,1,digit);
		case '6':return bit4ToDouble(0,1,1,0,digit);
		case '7':return bit4ToDouble(0,1,1,1,digit);
		case '8':return bit4ToDouble(1,0,0,0,digit);
		case '9':return bit4ToDouble(1,0,0,1,digit);
		case 'a':
		case 'A':return bit4ToDouble(1,0,1,0,digit);
		case 'b':
		case 'B':return bit4ToDouble(1,0,1,1,digit);
		case 'c':
		case 'C':return bit4ToDouble(1,1,0,0,digit);
		case 'd':		
		case 'D':return bit4ToDouble(1,1,0,1,digit);
		case 'e':
		case 'E':return bit4ToDouble(1,1,1,0,digit);
		case 'f':
		case 'F':return bit4ToDouble(1,1,1,1,digit);
		}
		return 0;
	}


	//16進数文字列から値を計算
	public static double hexStringToDouble(final String str)
	{
		double result=0;

		boolean minus=str.charAt(0)=='-';
		int dot=str.indexOf('.');

		for(int i=0;i<str.length();++i)
		{
			if(i==dot)
				continue;
			char c=str.charAt(i);
			int dig;
			if(dot==-1)
				dig=str.length()-i-1;
			else
			{
				dig=dot-i;
				if(i<dot)
					dig-=1;
			}


			result+=hexCharToDouble(c,dig);
		}
		return minus?-result:result;
	}

```
##doubleから16進数文字列への変換
```ruby:MyDouble.java
	//整数部分
	private static String I(int num)
	{
		String bit="";

		if(num==0)
			return "0";
		while(num>0)
		{

			bit+=Integer.toString(num%2);
			num/=2;
		}
		StringBuffer buf= new StringBuffer(bit);
		bit=buf.reverse().toString();

		return bit;
	}
	//少数部分
	private static String D(double num)
	{
		String bit="";

		if(num==0)
			return "0";
		while((0-num)!=0)
		{
			num*=2;
			bit+=(int)num;

			num-=(int)num;
		}
		StringBuffer buf= new StringBuffer(bit);
		bit=buf.reverse().toString();

		return bit;
	}
	//値から4bit分の数値取得
	private static int charToBit4(char b1,char b2,char b3,char b4)
	{
		return (int)(b1-48)*1000+(int)(b2-48)*100+(int)(b3-48)*10+(int)(b4-48);
	}
	//4bit分数値から16進数文字取得
	private static String bit4IntToHex(int bit4)
	{
		switch(bit4)
		{
		case 0:return "0";
		case 1:return "1";
		case 10:return "2";
		case 11:return "3";
		case 100:return "4";
		case 101:return "5";
		case 110:return "6";
		case 111:return "7";
		case 1000:return "8";
		case 1001:return "9";
		case 1010:return "A";
		case 1011:return "B";
		case 1100:return "C";
		case 1101:return "D";
		case 1110:return "E";
		case 1111:return "F";
		}
		return "";
	}
	
	//数値から16進数文字列取得
	static String doubleToHexString(double num)
	{
		boolean minus=num<0;
		num=Math.abs(num);
		String intbit=I((int)num);
		String intHex="";
		{		
			int numBitTable=(intbit.length()-1)/4+1;
			for(int i=intbit.length()-numBitTable*4,j=0;j<numBitTable;i+=4,++j)
			{
				char c1=i>=0?intbit.charAt(i):'0';
				char c2=i+1>=0?intbit.charAt(i+1):'0';
				char c3=i+2>=0?intbit.charAt(i+2):'0';
				char c4=i+3>=0?intbit.charAt(i+3):'0';
				int bit4=charToBit4(c1,c2,c3,c4);
				intHex+=bit4IntToHex(bit4);
			}

		}
		String dbit=D(num-(int)(num));
		String dHex="";
		{		
			int numBitTable=(dbit.length()-1)/4+1;
			for(int i=0,j=0;j<numBitTable;i+=4,++j)
			{
				char c1=i<dbit.length()?dbit.charAt(i):'0';
				char c2=i+1<dbit.length()?dbit.charAt(i+1):'0';
				char c3=i+2<dbit.length()?dbit.charAt(i+2):'0';
				char c4=i+3<dbit.length()?dbit.charAt(i+3):'0';
				int bit4=charToBit4(c1,c2,c3,c4);
				dHex+=bit4IntToHex(bit4);
			}

		}
		if(dHex.compareTo("0")!=0)
			dHex="."+dHex;
		else
		{
			dHex="";
		}
		return minus?"-"+intHex+dHex:intHex+dHex;
	}
```