RGB to int (after OP clarified)
You said you would like to convert your three RGB values to an integer. The integer ff1e00
(hexadecimal) which is 16719360
in decimal.
To do so, you can go directly from the RGB values using the following logic:
int hex = r << 16 | g << 8 | b;
Alternativly you can also convert your hexString
back to an integer:
int hex = Integer.parseInt(hexString, 16);
By the way, do not expext System.out.println(hex)
to print a hexadecimal value. It will be 16719360
, so decimal. int
does not store any information about its base. Integers are unaware of a base. They only come into play once you want to visualize them, i.e. go to a String
. And then you can just use the formatter, as you already figured out.
RGB to hex string (before OP clarified)
Your are very close. You already managed to convert your RGB values to a hexadecimal string FF1E00
. To get 0xff1e00
you simply have to add 0x
in front of it and make everything lowercase. Fortunately, both can be done compact with the formatter directly:
String.format("0x%02x%02x%02x", r, g, b);
I added 0x
in front of the format and changed your %...X
template to %...x
which gives lowercase-hexadecimal integers.
Alterntively you can also do easy string manipulation afterwards:
hexString = "0x" + hexStrong.toLowerCase();