Est*_*nes 3 java java-me parseint
在为J2ME编写游戏时,我们遇到了一个使用java.lang.Integer.parseInt()的问题
我们有几个常量值定义为十六进制值,例如:
CHARACTER_RED = 0xFFAAA005;
Run Code Online (Sandbox Code Playgroud)
在游戏过程中,值被序列化并通过网络连接接收,作为十六进制值的字符串表示形式.为了将其解析回int,我们不成功地尝试了以下内容:
// Response contains the value "ffAAA005" for "characterId"
string hexValue = response.get("characterId");
// The following throws a NumberFormatException
int value = Integer.parseInt(hexValue, 16);
Run Code Online (Sandbox Code Playgroud)
然后我运行了一些测试并尝试了这个:
string hexValue = Integer.toHexString(0xFFAAA005);
// The following throws a NumberFormatException
int value = Integer.parseInt(hexValue, 16);
Run Code Online (Sandbox Code Playgroud)
这是实际代码的例外:
java.lang.NumberFormatException: ffaaa005
at java.lang.Integer.parseInt(Integer.java:462)
at net.triadgames.acertijo.GameMIDlet.startMIDlet(GameMIDlet.java:109)
Run Code Online (Sandbox Code Playgroud)
我必须承认,这让我感到困惑.查看parseInt代码,当被解析的数字"越过""负/正边界"时,似乎抛出了NumberFormatException(也许有人可以用右边的行话来编辑?).
这是Integer.parseInt函数的预期行为吗?最后我不得不编写自己的十六进制字符串解析函数,我对提供的实现非常不满意.
换句话说,我期望让Integer.parseInt()在整数误导的十六进制字符串表示上工作吗?
编辑:在我最初的帖子中,我写了0xFFFAAA005而不是0xFFAAA005.我已经纠正了这个错误.
归档时间: |
|
查看次数: |
4970 次 |
最近记录: |