50

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?

7 Answers 7

98

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! I didn't know that Integer.parseInt(..) could take another parameter! Thanks for clearing that up for me!
not work java.lang.NumberFormatException: Invalid int: "0x920B540C", color2 = Integer.parseInt(color_2,16); (with argbA)
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).
I came here searching for an elegant solution for getting rid of 0x. So -1 for not mentionig Integer.decode().
47

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)

2 Comments

Best answer IMHO.
Just adding that this doesn't handle binary (0b...) prefixes
39

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");

6 Comments

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
@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 ?
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
But thanks for reply my vote+. now i got another solution : int color=Color.parseColor("#"+"803bb9ff");
Integer.decode was exactly what I needed! - it's basically parseInt but handles more variety of input formats
|
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);

Comments

3

Would this work?

Long.parseUnsignedLong(value, 16);

1 Comment

This worked for me. I was working with "800b6b0b": 2,148,231,947 Just over the 32 bit limit
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); 

Comments

0

In newer Java Versions (since 17) there is HexFormat:

     byte byteVal = (byte)HexFormat.of().fromHexDigits("7f");
     assert(byteStr.equals("7f"));
     assert(b == byteVal);

     HexFormat commaFormat = HexFormat.ofDelimiter(", ").withPrefix("#");
     byte[] bytes = {0, 1, 2, 3, 124, 125, 126, 127};
     String str = commaFormat.formatHex(bytes);

     byte[] parsed = commaFormat.parseHex(str);
     assert(Arrays.equals(bytes, parsed));

     // The formatted string is: "#00, #01, #02, #03, #7c, #7d, #7e, #7f"

https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/HexFormat.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.