Note that there are some explanatory texts on larger screens.

plurals
  1. POIs J2ME's Integer.parseInt() broken?
    text
    copied!<p>While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt()</p> <p>We have several constant values defined as hex values, for example:</p> <pre><code>CHARACTER_RED = 0xFFAAA005; </code></pre> <p>During the game the value is serialized and is received through a network connection, coming in as a string representation of the hex value. In order to parse it back to an int we unsuccesfully tried the following:</p> <pre><code>// Response contains the value "ffAAA005" for "characterId" string hexValue = response.get("characterId"); // The following throws a NumberFormatException int value = Integer.parseInt(hexValue, 16); </code></pre> <p>Then I ran some tests and tried this:</p> <pre><code>string hexValue = Integer.toHexString(0xFFAAA005); // The following throws a NumberFormatException int value = Integer.parseInt(hexValue, 16); </code></pre> <p>This is the exception from the actual code:</p> <pre><code>java.lang.NumberFormatException: ffaaa005 at java.lang.Integer.parseInt(Integer.java:462) at net.triadgames.acertijo.GameMIDlet.startMIDlet(GameMIDlet.java:109) </code></pre> <p>This I must admit, baffled me. Looking at the parseInt code the NumberFormatException seems to be thrown when the number being parsed "crosses" the "negative/positive boundary" (perhaps someone can edit in the right jargon for this?).</p> <p>Is this the expected behavior for the Integer.parseInt function? In the end I had to write my own hex string parsing function, and I was quite displeased with the provided implementation. </p> <p>In other words, was my expectation of having Integer.parseInt() work on the hex string representation of an integer misguided?</p> <p>EDIT: In my initial posting I wrote 0xFFFAAA005 instead of 0xFFAAA005. I've since corrected that mistake.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload