2014-09-29 2 views
0

긍정적 인 바이너리 입력을 16 진수로 변환하는 프로그램을 작성하려고합니다. 진수 변환기 내 바이너리를 컴파일하는 동안 이유는 .. 내 BinToHex 클래스Java.lang.NumberFormatException throw

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); 
    } 

}

내 테스트 클래스 다음

Exception in thread "main" java.lang.NumberFormatException: For input string: "148.0" 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
at java.lang.Long.parseLong(Long.java:441) 
at BinToHex.convertbintohex(BinToHex.java:24) 
at Test.main(Test.java:4) 

입니다 ..이 오류를 얻고있다

public class Test{ 
public static void main(String args[]){ 
    BinToHex i = new BinToHex(); 
    i.convertbintohex();  
} 

}

이러한 질문 ;-) 죄송

정말 " 긴 서명 로 문자열 인수를 구문 분석"

+1

이것은 컴파일러 오류가 아닙니다. –

+3

문자열 '148.0'은'Long '으로 구문 분석 될 수 없습니다. –

+0

왜 문자열을 long으로 구문 분석 할 수 없습니까? 이것의 해결책은 무엇입니까? @SotiriosDelimanolis – Tiash

답변

0

Long.parseLong를 도움이 필요합니다. 148.0은 두 배입니다.

당신은 문,하지만 이후 "만일"의 캐스트를 사용하는 대신

if ((int)tempDec == tempDec) { 
    tempDecString = String.valueOf(tempDec); 
    templongDec = Long.parseLong(tempDecString, 10); 

, 시도 :

if ((long)tempDec == tempDec) { 
    tempDecString = String.valueOf((long)tempDec); 
    templongDec = Long.parseLong(tempDecString, 10); 

가 이미하고있는 것을 나중에 다른 사람에 성명서, 당신은 방금 그것을 놓쳤습니다. 이 코드에는 다른 문제가 있다고 생각하지만 원래 질문에 대답해야합니다.

관련 문제