Byte.parseByte throws NumberFormatException for value "95", radix: 16
I have question about parsing hex string. Java 6u25 When I call Byte.parseByte("95", 16) I get. java.lang.NumberFormatException: Value out of range. Value:"95" Radix:16 at java.lang.Byte.parseByte(Byte.java:153) 95 hex is 149 dec, Integer value '149' can be cast to valid byte value -107 Is the exception correct? Thank you very much for answer Martin JANDA jandam@crcdata.cz Code from Byte.java public static byte parseByte(String s, int radix) throws NumberFormatException { int i = Integer.parseInt(s, radix); if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException( "Value out of range. Value:\"" + s + "\" Radix:" + radix); return (byte)i; }
* Janda Martin:
Is the exception correct?
I think so. The specification says this: | The value represented by the string is not a value of type byte. -- Florian Weimer <fweimer@bfk.de> BFK edv-consulting GmbH http://www.bfk.de/ Kriegsstraße 100 tel: +49-721-96201-1 D-76133 Karlsruhe fax: +49-721-96201-99
Janda Martin said the following on 07/12/11 21:32:
I have question about parsing hex string. Java 6u25
When I call Byte.parseByte("95", 16) I get.
java.lang.NumberFormatException: Value out of range. Value:"95" Radix:16 at java.lang.Byte.parseByte(Byte.java:153)
95 hex is 149 dec, Integer value '149' can be cast to valid byte value -107
Is the exception correct?
Yes. 95 hex == 149 dec which is > 127 and so is out of range for byte. If you want to use the bit pattern of 95 hex as a byte then you need to parse it as an int and do the cast yourself. David Holmes
Thank you very much for answer
Martin JANDA jandam@crcdata.cz
Code from Byte.java
public static byte parseByte(String s, int radix) throws NumberFormatException { int i = Integer.parseInt(s, radix); if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException( "Value out of range. Value:\"" + s + "\" Radix:" + radix); return (byte)i; }
Thank you very much for fast reply. I use (byte)Integer.parseInt.... Martin ----- Original Message ----- From: "David Holmes" <David.Holmes@oracle.com> To: "Janda Martin" <jandam@crcdata.cz> Cc: "core-libs-dev" <core-libs-dev@openjdk.java.net> Sent: Tuesday, July 12, 2011 2:19:25 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: Re: Byte.parseByte throws NumberFormatException for value "95", radix: 16 Janda Martin said the following on 07/12/11 21:32:
I have question about parsing hex string. Java 6u25
When I call Byte.parseByte("95", 16) I get.
java.lang.NumberFormatException: Value out of range. Value:"95" Radix:16 at java.lang.Byte.parseByte(Byte.java:153)
95 hex is 149 dec, Integer value '149' can be cast to valid byte value -107
Is the exception correct?
Yes. 95 hex == 149 dec which is > 127 and so is out of range for byte. If you want to use the bit pattern of 95 hex as a byte then you need to parse it as an int and do the cast yourself. David Holmes
Thank you very much for answer
Martin JANDA jandam@crcdata.cz
Code from Byte.java
public static byte parseByte(String s, int radix) throws NumberFormatException { int i = Integer.parseInt(s, radix); if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException( "Value out of range. Value:\"" + s + "\" Radix:" + radix); return (byte)i; }
participants (3)
-
David Holmes
-
Florian Weimer
-
Janda Martin