我有以下代碼十六進制到二進制轉換的java
temp = "0x00"
String binAddr = Integer.toBinaryString(Integer.parseInt(temp, 16));
爲什麼我會收到以下錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0x00"
我有以下代碼十六進制到二進制轉換的java
temp = "0x00"
String binAddr = Integer.toBinaryString(Integer.parseInt(temp, 16));
爲什麼我會收到以下錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "0x00"
由於字符串包含0x
,使用Integer.decode(String nm):
String binAddr = Integer.toBinaryString(Integer.decode(temp));
這給了我再次..線程「AWT-EventQueue-0」中的異常java.lang.NumberFormatException:對於輸入字符串:「0x00」 – user2761225
String binAddr = Integer.toBinaryString(Integer.decode(temp));再次錯誤...字符串溫度是「0x00」 – user2761225
@ user2761225這對我來說很好。再次檢查,你可能在其他地方做錯了事。 –
因爲領先0x
是不是一個有效的基本-16的部分數字 - 這只是一個約定,向讀者指出數字是十六進制的。
所以如果我不子第一次兩個好嗎? – user2761225
是的 - 閱讀[Javadocs](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.htm) –
你知道如何獲得8位二進制轉換我的意思是那0x00是00000000不是0 ?? – user2761225
擺脫 '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.
的0x
爲整數文字,例如:
int num = 0xCAFEBABE;
但不是解析的格式。試試這個:
temp = "ABFAB"; // without the "0x"
String binAddr = Integer.toBinaryString(Integer.parseInt(temp, 16));
http://stackoverflow.com/questions/19488067/charsequence-to-integer-with-multiple-ve-and-ve-signss/19488112#19488112 –