2013-10-21 101 views
0

我有以下代碼十六進制到二進制轉換的java

temp = "0x00" 
String binAddr = Integer.toBinaryString(Integer.parseInt(temp, 16)); 

爲什麼我會收到以下錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0x00"

+1

http://stackoverflow.com/questions/19488067/charsequence-to-integer-with-multiple-ve-and-ve-signss/19488112#19488112 –

回答

1

由於字符串包含0x,使用Integer.decode(String nm)

String binAddr = Integer.toBinaryString(Integer.decode(temp)); 
+0

這給了我再次..線程「AWT-EventQueue-0」中的異常java.lang.NumberFormatException:對於輸入字符串:「0x00」 – user2761225

+0

String binAddr = Integer.toBinaryString(Integer.decode(temp));再次錯誤...字符串溫度是「0x00」 – user2761225

+1

@ user2761225這對我來說很好。再次檢查,你可能在其他地方做錯了事。 –

0

因爲領先0x是不是一個有效的基本-16的部分數字 - 這只是一個約定,向讀者指出數字是十六進制的。

+0

所以如果我不子第一次兩個好嗎? – user2761225

+0

是的 - 閱讀[Javadocs](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.htm) –

+0

你知道如何獲得8位二進制轉換我的意思是那0x00是00000000不是0 ?? – user2761225

0

擺脫 '0X' 的:從的javadoc:

The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned.

0

0x爲整數文字,例如:

int num = 0xCAFEBABE; 

但不是解析的格式。試試這個:

temp = "ABFAB"; // without the "0x" 
String binAddr = Integer.toBinaryString(Integer.parseInt(temp, 16)); 
相關問題