Integer to RGB Converter
Unpack a 24-bit integer (0xRRGGBB) into its red, green, and blue channel values.
How to use this tool
- Enter a 24-bit integer (0–16777215).
- The tool extracts the red, green, and blue channels via bit-shifts.
- You also get the equivalent hex color code.
Unpack a 24-bit integer color value into its R, G, B channels and hex string. Useful when working with game engines or low-level graphics.
Formula
Given a 24-bit integer n in the range 0–16,777,215 (0x000000–0xFFFFFF):
R = (n >> 16) & 0xFF
G = (n >> 8) & 0xFF
B = n & 0xFF
Equivalently: R = floor(n / 65536), G = floor((n mod 65536) / 256), B = n mod 256.
How it works
The input integer is clamped to the 24-bit range (0–16,777,215). Right-shifting by 16 bits and masking with 0xFF isolates the red byte; shifting by 8 and masking isolates green; masking alone isolates the blue byte in the lowest 8 bits. The hex string is zero-padded to exactly six digits and prefixed with #.
Common mistake: passing a 32-bit ARGB integer (which includes an alpha channel in the highest byte) instead of a 24-bit RGB integer. For example, fully-opaque red in ARGB32 is 0xFFFF0000 = 4,278,190,080, which far exceeds the 24-bit maximum and will be clamped, producing a wrong color. Strip the alpha byte first if your source uses 32-bit color.
Worked example
Unpack 16711680 into RGB channels
- Clamp input: 16,711,680 is within 0–16,777,215.
- Red: 16,711,680 >> 16 = 255; 255 & 0xFF = 255.
- Green: 16,711,680 >> 8 = 65,280; 65,280 & 0xFF = 0.
- Blue: 16,711,680 & 0xFF = 0. Hex: #ff0000.
R: 255, G: 0, B: 0, Hex: #ff0000
Common mistakes to avoid
- Passing a signed 32-bit integer (e.g. a negative value from Java or C#) instead of an unsigned 24-bit value, which corrupts all three channels.
- Forgetting to mask with 0xFF after the right-shift, so green and red bleed into lower channels.
- Confusing big-endian 0xRRGGBB with little-endian 0xBBGGRR byte order used in some older Windows COLORREF values.
Key terms
- 24-bit integer
- A color value stored as one number in the range 0–16,777,215 where each of the three 8-bit channels (R, G, B) occupies 8 consecutive bits in the 0xRRGGBB arrangement.
- Right bit shift (>>)
- Moving binary digits rightward, effectively dividing by a power of 2 and discarding the remainder. Shifting n right by 16 moves the red byte from bits 16–23 into bits 0–7 for easy extraction.
- Bitmask (& 0xFF)
- A bitwise AND with 255 (binary 11111111) that keeps only the lowest 8 bits of a number and zeroes out everything above. Used after shifting to isolate exactly one color channel.
- ARGB / RGBA
- 32-bit color formats that add a fourth alpha (transparency) channel. ARGB stores alpha in the top byte (0xAARRGGBB) and RGBA in the bottom byte (0xRRGGBBAA). Neither is a plain 24-bit RGB integer.
Frequently asked questions
- How do you extract RGB from an integer?
- R = (n >> 16) & 0xFF, G = (n >> 8) & 0xFF, B = n & 0xFF.
- What is 16777215?
- It is 0xFFFFFF in hex, which is pure white (255, 255, 255).