1

See how int a and int b have the prefix "0x" before the hex value? I want to make more of these from a string. I tried to use parseInt from Integer, but I get a NumberFormatException. Can anyone help?

int a = 0xA;
int b = 0x4;
String f = "0xF";    
int d = Integer.parseInt(f, 16);

I want "int d = 0xF"

Archer
  • 111
  • 3
  • 10

2 Answers2

6

How about this?

String f = "0xF";    
int d = Integer.parseInt(f.substring(2), 16);
System.out.println(d);

OUTPUT:

15
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
3

Try:

Integer.decode("0x64");

A similar question was asked before in SO, here's a link for more info:

Parsing integer strings in Java

Community
  • 1
  • 1
ali
  • 381
  • 2
  • 12