I am trying to write a program for converting positive binary inputs into hex.
Why am i getting this errors while compiling my binary to hex converter..
Exception in thread "main" java.lang.NumberFormatException: For input string: "148.0"
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at BinToHex.convertbintohex(BinToHex.java:24)
at Test.main(Test.java:4)
Here is my BinToHex class
import java.io.*; public class BinToHex { double tempDec,fractionpart; long longofintpart,templongDec; String inpu ="1001.01"; String hexOutput,intpart,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 ((int)tempDec == tempDec) { tempDecString = String.valueOf(tempDec); templongDec = Long.parseLong(tempDecString, 10); hexOutput = Long.toHexString(templongDec); break; } else { intpart = String.valueOf((long)tempDec); longofintpart = Long.valueOf(intpart).longValue(); if(i==1){ hex=Long.toHexString(longofintpart); 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); } }
and my Test class..
public class Test{ public static void main(String args[]){ BinToHex i = new BinToHex(); i.convertbintohex(); } }
my first question
really need help