Byte.parseByte() don't work!

49 views
Skip to first unread message

Nikita Kapitonov

unread,
Nov 26, 2014, 3:49:57 PM11/26/14
to java...@googlegroups.com
I couldn't find it anywhere on the Internet, also this fact is not described in our FAQ.

parseByte(String s, int radix)

^^^
don't work when the result will be negative, it would throw java.lang.NumberFormatException.
Not dependant on lengh of String, radix, or inclusions of letters A...F.

For example, the following expression don't work:
Byte.parseByte("9A", 16);

The workaround is to use "(byte) Integer.parseInt(args)" with the same arguments.

I've tried this on the TC65i-X, but I'm sure this would be related to all the older chips also.

What do you think?
Can you check on your chip?
Do you have some other internal java methods that simply don't work?

Full list of trials, source code in attach:

Integer.parseInt("99")=99
Integer.parseInt("99", 10)=99
Integer.parseInt("99", 16)=153
Integer.parseInt("9A")=FAIL --- OK
Integer.parseInt("9A", 10)=FAIL --- OK
Integer.parseInt("9A", 16)=154
Byte.parseByte("99")=99
Byte.parseByte("99", 10)=99

This should produce -56
Byte.parseByte("200")=java.lang.NumberFormatException
 - java.lang.Byte.parseByte(), bci=22
 - java.lang.Byte.parseByte(), bci=3
 - parsebytetest.IMlet.startApp(), bci=415
 - javax.microedition.midlet.Main.run(), bci=3

This should produce -56
Byte.parseByte("200", 10)=java.lang.NumberFormatException
 - java.lang.Byte.parseByte(), bci=22
 - parsebytetest.IMlet.startApp(), bci=464
 - javax.microedition.midlet.Main.run(), bci=3

This should produce -103
Byte.parseByte("99", 16)=java.lang.NumberFormatException
 - java.lang.Byte.parseByte(), bci=22
 - parsebytetest.IMlet.startApp(), bci=513
 - javax.microedition.midlet.Main.run(), bci=3

Byte.parseByte("9A")=FAIL --- OK
Byte.parseByte("9A", 10)=FAIL --- OK

This should produce -102
Byte.parseByte("9A", 16)=java.lang.NumberFormatException
 - java.lang.Byte.parseByte(), bci=22
 - parsebytetest.IMlet.startApp(), bci=666
 - javax.microedition.midlet.Main.run(), bci=3

Byte.parseByte("1A", 16)=26
Byte.parseByte("A", 16)=10
Byte.parseByte("0A", 16)=10
Integer.toHexString(26)=1a

IMlet.java

Nikita Kapitonov

unread,
Nov 26, 2014, 5:37:27 PM11/26/14
to java...@googlegroups.com
Maybe I was too quick to assume it won't work on all the negative results, because it does work when you put a minus sign like
parseByte("-0A",16) = -10
Anyways, they should have put this information somewhere.

So, the final precaution is:

Do not use Byte.parseByte() to parse bytes from hex-strings!

среда, 26 ноября 2014 г., 23:49:57 UTC+3 пользователь Nikita Kapitonov написал:
Reply all
Reply to author
Forward
0 new messages