48

So, In Java, you know how you can declare integers like this:

int hex = 0x00ff00;

I thought that you should be able to reverse that process. I have this code:

Integer.valueOf(primary.getFullHex());

where primary is an object of a custom Color class. It's constructor takes an Integer for opacity (0-99) and a hex String (e.g. 00ff00).

This is the getFullHex method:

public String getFullHex() {
    return ("0x" + hex);
}

When I call this method it gives my this NumberFormatException:

java.lang.NumberFormatException: For input string: "0xff0000"

I can't understand what's going on. Can someone please explain?

mattbdean
  • 2,532
  • 5
  • 26
  • 58

6 Answers6

97

Will this help?

Integer.parseInt("00ff00", 16)

16 means that you should interpret the string as 16-based (hexadecimal). By using 2 you can parse binary number, 8 stands for octal. 10 is default and parses decimal numbers.

In your case Integer.parseInt(primary.getFullHex(), 16) won't work due to 0x prefix prepended by getFullHex() - get rid of and you'll be fine.

Oliv
  • 10,221
  • 3
  • 55
  • 76
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 1
    Thank you! I didn't know that `Integer.parseInt(..)` could take another parameter! Thanks for clearing that up for me! – mattbdean Jul 07 '12 at 22:57
  • not work java.lang.NumberFormatException: Invalid int: "0x920B540C", color2 = Integer.parseInt(color_2,16); (with argbA) –  Aug 05 '15 at 15:08
  • 1
    Even if you remove the `0x` prefix from the input string `"0x920B540C"` and parse it in base 16, hex `920B540C` is decimal `2450215948`, which is more than the max value that a 32bit signed `int` can hold (dec `2147483647`, hex `0x7FFFFFFF` - see [`Integer.MAX_VALUE`](https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE)). You would have to use a 64bit signed `long` instead (Java doesn't have unsigned integer types). – Remy Lebeau Dec 13 '17 at 02:52
  • 2
    I came here searching for an elegant solution for _getting rid of 0x_. So -1 for not mentionig `Integer.decode()`. – Line Aug 17 '18 at 18:37
40

Integer.valueOf(string) assumes a decimal representation. You have to specify that the number is in hex format, e.g.

int value = Integer.valueOf("00ff0000", 16); 

Note that Integer.valueOf(string,16); does not accept a prefix of 0x. If your string contains the 0x prefix, you can use Integer.decode("0x00ff0000");

nos
  • 223,662
  • 58
  • 417
  • 506
  • 1
    Hey this is working only for 00ff0000 not for 80ff0000 . I have string like String hex="803BB9FF"; and i want to covert this into int color=0x803BB9FF please help – Ashish Sahu May 24 '14 at 01:48
  • @AshishSahu It's impossible to help when you don't describe what happens, and what you expect to happen. 0x803BB9FF is -2143569409 (since int in Java is signed). So what's "not working" about -2143569409 ? – nos May 24 '14 at 01:52
  • I have 8char color string like "803BB9FF" now i want to convert it in int value like 0x803BB9FF (int) i tryied with "003bb9ff " then it's working but not working with 80 prefix – Ashish Sahu May 24 '14 at 02:07
  • 3
    But thanks for reply my vote+. now i got another solution : int color=Color.parseColor("#"+"803bb9ff"); – Ashish Sahu May 24 '14 at 02:11
  • 3
    `Integer.decode` was exactly what I needed! - it's basically parseInt but handles more variety of input formats – Krease Sep 28 '15 at 22:25
  • `0x803BB9FF` is larger than `Integer.MAX_VALUE` (`0x7FFFFFFF`), that is why `Integer.parseInt()` and `Integer.decode()` can't handle it. Use `Long` instead. – Remy Lebeau Dec 13 '17 at 02:57
39

Try to use decode method:

Integer.decode("0x00ff00");

DecodableString:

  • Signopt DecimalNumeral
  • Signopt 0x HexDigits
  • Signopt 0X HexDigits
  • Signopt # HexDigits
  • Signopt 0 OctalDigits

You can read about it here https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#decode(java.lang.String)

Konstantin Labun
  • 3,688
  • 24
  • 24
3

The parseInt method only accepts the number part, not any kind of "base" indicator such as "0x" for hexadecimal or "0" for octal. Use it like this

int decimal = Integer.parseInt("1234", 10);
int octal = Integer.parseInt("1234", 8);
int hex = Integer.parseInt("1234", 16);
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
2

Would this work?

Long.parseUnsignedLong(value, 16);
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
Matt
  • 445
  • 4
  • 3
  • This worked for me. I was working with "800b6b0b": 2,148,231,947 Just over the 32 bit limit – indigo Jun 29 '22 at 21:43
0

This should do it:

String hex = "FA"; 
int intValue = Integer.parseInt(hex, 16);

And if you want to generate hex representation of an integer, use

String hex = Integer.toHexString(12); 
GETah
  • 20,922
  • 7
  • 61
  • 103