0

I have:

int number = 0x00000024;

And it have no problems, but now im get it from with de format:

<MAN>
      <NAME hexa="0x00000001"/>
</MAN>

And I try to parse hexa with:

Integer.parseInt(parser.getAttributeValue(0), 16)

But it says:

Unable to parse '0x00000001' as integer

Anyone knows whats happening?

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162
  • 1
    See also this question: http://stackoverflow.com/questions/5153811/how-to-convert-a-hexadecimal-string-to-long-in-java – DRCB Apr 16 '12 at 14:42
  • http://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java – scibuff Apr 16 '12 at 14:42

2 Answers2

6

Remove the 0x:

Integer.parseInt(parser.getAttributeValue(0).substring(2), 16)
MByD
  • 135,866
  • 28
  • 264
  • 277
  • 3
    Actually, the answer in @DRCB's link is even better Integer.decode: http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#decode(java.lang.String) – MByD Apr 16 '12 at 14:45
0

Building on the answer in How to convert a hexadecimal string to long in java?, you want:

int num = Long.decode(parser.getAttributeValue(0)).intValue();
Community
  • 1
  • 1
trutheality
  • 23,114
  • 6
  • 54
  • 68