I am trying to write a program for converting positive binary inputs into hex. in the hex output the point (".")is missing.
Suppose my expected output is e7.6 , but i am getting e76.
only the "." is missing.
here is my BinToHex class..
my main Test class..import java.io.*; public class BinToHex { double tempDec,fractionpart; long longofintpart,templongDec; String inpu ="11100111.011"; String hexOutput=null,tempDecString,hex = null; static int i = 1; public void convertbintohex() { if (inpu.contains(".")) { int placesAfterPoint = inpu.length() - inpu.indexOf(".") - 1;//every thing long numerator = Long.parseLong(inpu.replace(".", ""), 2);//goes double decimalOfInput = ((double) numerator) / (1L << placesAfterPoint);//alright till here while (true) { tempDec = decimalOfInput * 16; if (tempDec == (int)tempDec) { tempDecString = String.valueOf((long)tempDec); templongDec = Long.parseLong(tempDecString, 10); hexOutput = Long.toHexString(templongDec); break; } else { longofintpart = (long)tempDec; hex=Long.toHexString(longofintpart); if(i==1){ hexOutput = hex + "."; i=i+1; }else{ hexOutput = hexOutput + hex; } fractionpart = tempDec-(int)tempDec; decimalOfInput = fractionpart; } } } else { // this part is ok tempDecString = String.valueOf(Integer.parseInt(inpu, 2)); templongDec = Long.parseLong(tempDecString, 10); hexOutput = Long.toHexString(templongDec); } System.out.println(hexOutput); } }
public class Test{ public static void main(String args[]){ BinToHex i = new BinToHex(); i.convertbintohex(); } }
here is how decimal fraction is converted into hex
or here too
plz help me.