How to convert a color integer to a hex String in Android?

Bosah Chude picture Bosah Chude · Jun 30, 2011 · Viewed 104.8k times · Source

I have an integer that was generated from an android.graphics.Color

The Integer has a value of -16776961

How do I convert this value into a hex string with the format #RRGGBB

Simply put: I would like to output #0000FF from -16776961

Note: I do not want the output to contain an alpha and i have also tried this example without any success

Answer

Josh picture Josh · Jun 30, 2011

The mask makes sure you only get RRGGBB, and the %06X gives you zero-padded hex (always 6 chars long):

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));