Parsing a Hexadecimal String to an Integer throws a NumberFormatException?
Asked Answered
P

6

49

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?

Pettifogger answered 7/7, 2012 at 19:17 Comment(0)
L
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.

Librate answered 7/7, 2012 at 19:20 Comment(4)
Thank you! I didn't know that Integer.parseInt(..) could take another parameter! Thanks for clearing that up for me!Pettifogger
not work java.lang.NumberFormatException: Invalid int: "0x920B540C", color2 = Integer.parseInt(color_2,16); (with argbA)Ortego
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). You would have to use a 64bit signed long instead (Java doesn't have unsigned integer types).Duston
I came here searching for an elegant solution for getting rid of 0x. So -1 for not mentionig Integer.decode().Consequence
J
43

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)

Jermyn answered 16/8, 2016 at 13:15 Comment(2)
Best answer IMHO.Councilwoman
Just adding that this doesn't handle binary (0b...) prefixesTitfer
M
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");

Mckibben answered 7/7, 2012 at 19:24 Comment(6)
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 helpRelator
@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 ?Mckibben
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 prefixRelator
But thanks for reply my vote+. now i got another solution : int color=Color.parseColor("#"+"803bb9ff");Relator
Integer.decode was exactly what I needed! - it's basically parseInt but handles more variety of input formatsIdiographic
0x803BB9FF is larger than Integer.MAX_VALUE (0x7FFFFFFF), that is why Integer.parseInt() and Integer.decode() can't handle it. Use Long instead.Duston
C
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);
Cinelli answered 7/7, 2012 at 19:24 Comment(0)
H
3

Would this work?

Long.parseUnsignedLong(value, 16);
Hogweed answered 7/8, 2019 at 20:38 Comment(1)
This worked for me. I was working with "800b6b0b": 2,148,231,947 Just over the 32 bit limitGerman
O
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); 
Oblation answered 7/7, 2012 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.