Android
Color Conversion
Hexadecimal
Programming
Mobile Development

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Android colors are often stored as 32-bit integers in ARGB format. If you want to display them, log them, or serialize them in a human-readable way, you usually need to convert that integer into a hexadecimal string such as #FF336699.

The conversion itself is simple, but you need to decide whether to include alpha and whether the output should be uppercase, lowercase, or prefixed with #. Those formatting choices are usually more important than the integer math.

Convert ARGB Int to Hex String

A standard Android color integer includes alpha, red, green, and blue channels. The easiest way to turn it into hex is String.format:

java
int color = 0xFF336699;
String hex = String.format("#%08X", color);
System.out.println(hex);

This produces:

text
#FF336699

The %08X means:

  • use hexadecimal
  • use uppercase letters
  • pad to 8 characters with leading zeroes

That is the usual answer when you want the full ARGB form.

Output RGB Only

Sometimes you do not want alpha in the final string. In that case, mask off the upper byte:

java
int color = 0xFF336699;
String rgbHex = String.format("#%06X", 0xFFFFFF & color);
System.out.println(rgbHex);

This produces:

text
#336699

That is useful when the receiving code assumes opaque colors or only wants the visible RGB portion.

Kotlin Version

The same formatting works cleanly in Kotlin:

kotlin
1val color = 0xFF336699.toInt()
2val argb = String.format("#%08X", color)
3val rgb = String.format("#%06X", 0xFFFFFF and color)
4
5println(argb)
6println(rgb)

If you are already in Kotlin, this is often the simplest direct solution.

Use Color Helpers When You Need Components

If you need to inspect channels separately before formatting, Android's Color helpers make that clearer:

java
1import android.graphics.Color;
2
3int color = 0x80336699;
4int alpha = Color.alpha(color);
5int red = Color.red(color);
6int green = Color.green(color);
7int blue = Color.blue(color);
8
9String hex = String.format("#%02X%02X%02X%02X", alpha, red, green, blue);
10System.out.println(hex);

This is useful when the color did not start as a packed integer literal and you want explicit control over the output order.

Be Careful with Signed Ints

Android color ints are often negative when viewed as ordinary Java integers because the highest bit may be set. That does not mean the color is wrong.

For example:

java
int color = Color.parseColor("#FF0000");
System.out.println(color);
System.out.println(String.format("#%08X", color));

The decimal value may be negative, but the formatted hex string will still correctly show the ARGB bytes.

Pick One Output Format and Stay Consistent

Teams often run into confusion because one layer stores #RRGGBB while another expects #AARRGGBB. Decide on one format for logging, persistence, and API boundaries, then keep the conversion logic in one place. That avoids subtle bugs where a transparent color is accidentally treated as fully opaque, or the first byte is misread as red instead of alpha.

Common Pitfalls

  • Forgetting whether the output should include alpha.
  • Using %06X when the color actually needs full ARGB output.
  • Being surprised that a color int prints as a negative decimal number.
  • Forgetting the # prefix when the result will be reused in color-parsing APIs.
  • Manually shifting channels when String.format and Color helpers already solve the problem clearly.

Summary

  • Android colors are usually packed ARGB integers.
  • Use String.format("#%08X", color) for full ARGB output.
  • Use 0xFFFFFF & color with %06X when you want RGB only.
  • Negative decimal color values are normal for some ARGB ints.
  • Use Color.alpha, Color.red, Color.green, and Color.blue when channel-level control is useful.

Course illustration
Course illustration

All Rights Reserved.