Unable to parse ARGB hex to integer
Question
I'm trying to parse an ARGB hex String
to an int
but it's not working.
My code:
int color = Integer.parseInt("ff686868", 16);
Exception:
java.lang.NumberFormatException: unable to parse 'ff686868' as integer
at java.lang.Integer.parse(Integer.java:438)
at java.lang.Integer.parseInt(Integer.java:422)
...
When I try to parse a normal RGB String
like 686868
it does work but when I add the alpha it breaks down. I hope someone can help me.
Solution
The number would be too big for integer.
Use: Long.parseLong(..)
OTHER TIPS
Use java.lang.Long.parseLong()
— int
is signed so it is only 31 bits + 1 sign bit, and you use all 4 * 8 = 32 bits.
You can use Color.parseColor(String)
to get the int
Color.parseColor(String);
if itz hexacode do this
Color.parseColor("#ffffff");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow