2014-03-25 94 views
1

,當我有功課從BIN轉換爲十六進制,我寫了下面的算法中的代碼:的Java:NumberFormatException的轉換二進制到十六進制

import java.util.Scanner; 

public class BinaryToHex { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Binary number: "); 
     String b = input.next(); 
     int bin = Integer.parseInt(b); 
     int arrlength = b.length(); 

     while (arrlength%4 != 0){ 
      arrlength++; 
     } 

     int[] arrbin = new int [arrlength]; 
     int digit = 0; 
     String hex = ""; 
     String str; 
     int conv; 

     for (int i = arrlength-1; i>=0; i--){ 
      digit = bin%10; 
      arrbin[i]=digit; 
      bin = bin/10; 
     } 

     System.out.print("Hex value = "); 

     for (int index = 0; index < arrlength; index=index+4){ 
      str = "" + arrbin[index] + "" + arrbin[index+1] + "" + arrbin[index+2] + "" + arrbin[index+3]; 
      switch(str){ 
      case "0000": str = "0"; break; 
      case "0001": str = "1"; break; 
      case "0010": str = "2"; break; 
      case "0011": str = "3"; break; 
      case "0100": str = "4"; break; 
      case "0101": str = "5"; break; 
      case "0110": str = "6"; break; 
      case "0111": str = "7"; break; 
      case "1000": str = "8"; break; 
      case "1001": str = "9"; break; 
      case "1010": str = "A"; break; 
      case "1011": str = "B"; break; 
      case "1100": str = "C"; break; 
      case "1101": str = "D"; break; 
      case "1110": str = "E"; break; 
      case "1111": str = "F"; break; 
      } 
      System.out.print(str); 
     } 
    } 

} 

的問題是,當我試圖把它轉換拋出更大的數字:

Exception in thread "main" java.lang.NumberFormatException: For input string: "10101010101010" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at BinaryToHex.main(BinaryToHex.java:9) 

我知道問題與int類型有關,但我無法弄清楚如何解決這個問題。我試圖使用長型 - 結果是一樣的。

我將不勝感激,如果你們,可以幫助我糾正這段代碼,以便使用更大的數字。

回答

2

在Java中的int是32位有符號所以它的範圍是

-2,147,483,648 to 2,147,483,647 

所以你的10101010101010值是在這個範圍之外。

嘗試使用有點像龍

long bin = Long.valueOf("10101010101010"); 
System.out.println(bin); 

較大見Primitive Data Types

+0

謝謝,它的作品!這就是我一直在尋找的東西!我後悔我不允許爲你投票......:| – Kirev

0

我做得很快,很髒。應適用於所有長度:

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.print("Binary number: "); 
    String b = input.next(); 
    while (b.length() % 4 != 0) b = "0" + b; 
    StringBuilder builder = new StringBuilder(); 
    for (int count = 0; count < b.length(); count += 4) { 
     String nibble = b.substring(count, count + 4); 
     builder.append(Integer.toHexString(Integer.parseInt(nibble, 2))); 
    } 
    System.out.println(builder); 
} 
+0

是的,幹得好!這也是一個很好的方法。 :) – Kirev

0

這個數字確實太大了。如果您需要如此大的數字,請使用Long.parseLong()long類型而不是int

編輯:

剛纔我明白你真正想要解析二進制數。所以,使用Integer.parseInt(str, 2)。否則,該數字被解釋爲十進制。

0

我用這種方法轉換

public static String binaryToHex(String bin) { 
    return String.format("%21X", Long.parseLong(bin,2)) ; 
} 
0

如果輸入字符串爲最大32位值也沒有必要使用長。

-- for any binary input till 32 bit lenght following will work 
Integer.parseInt(yourBinaryString, 2) 

-- for any binary input till 64 bit lenght following will work 
Long.parseLong(yourBinaryString, 2) 

-- for longer binary string input values have a look at BigInteger 
相關問題