Coding 101

Mastering Hex Codes: Complete Hexadecimal, Binary, ASCII Lookup Table, and Modern CSS Color Spaces

DD
Ankur Ishwar
19 min read Updated Nov 17, 2026
Hex Codes: Complete Hexadecimal, Binary, ASCII, and CSS Color Guide
Interactive Developer Utility

Need to Convert, Inspect, or Test Hex Codes Live?

Use our free, client-side Hex Color Converter: real-time RGB, HSL, OKLCH, 8-digit alpha opacity slider, and WCAG 2.1 contrast checking.

Launch Hex Converter →

The Fundamentals of Hexadecimal Notation

Computers do not think in base-10 decimals or human alphabets. They process voltage differentials in binary ones and zeros. Reading raw binary strings like 1101101011111110 quickly overwhelms human comprehension. Base-16 hexadecimal notation (hex) solves this cognitive bottleneck by compressing four binary bits (one nibble) into a single alphanumeric glyph.

Hexadecimal uses sixteen distinct symbols: digits 0 through 9 represent values zero to nine, and letters A through F represent values ten through fifteen. Because exactly 24 = 16 states exist in four bits, two hexadecimal characters map cleanly to one standard 8-bit byte (28 = 256 states, spanning 0x00 to 0xFF). This direct correspondence makes hex notation foundational across memory addressing, packet sniffing, cryptography, character encoding, and web color systems.

Table 1: The 4-Bit Nibble Lookup Matrix

Every single byte consists of two 4-bit nibbles: a high nibble and a low nibble. Below is the fundamental translation matrix connecting decimal integers, hexadecimal characters, and their raw 4-bit binary equivalents:

Decimal Value Hexadecimal Character Binary Nibble (4-bit) Bit Weight (8-4-2-1)
0000000 + 0 + 0 + 0
1100010 + 0 + 0 + 1
2200100 + 0 + 2 + 0
3300110 + 0 + 2 + 1
4401000 + 4 + 0 + 0
5501010 + 4 + 0 + 1
6601100 + 4 + 2 + 0
7701110 + 4 + 2 + 1
8810008 + 0 + 0 + 0
9910018 + 0 + 0 + 1
10A10108 + 0 + 2 + 0
11B10118 + 0 + 2 + 1
12C11008 + 4 + 0 + 0
13D11018 + 4 + 0 + 1
14E11108 + 4 + 2 + 0
15F11118 + 4 + 2 + 1

Table 2: Hexadecimal Byte Mapping (0 to 255)

In computer architecture and network protocols, data is addressed in bytes. A single byte ranges from decimal 0 (0x00) to 255 (0xFF). Here is how decimal increments translate across common engineering byte thresholds:

Decimal Byte Hex Notation 8-Bit Binary String System Architecture Context
00x0000000000Null byte / String terminator (\0)
160x1000010000First two-digit decimal milestone
320x2000100000ASCII Space character
640x4001000000ASCII '@' symbol / 64-byte memory boundary
1270x7F01111111Maximum positive signed 8-bit integer
1280x8010000000High bit set / Extended ASCII entry
1920xC011000000Common subnet octet (192.168.x.x)
2550xFF11111111Maximum unsigned 8-bit byte / Color channel full saturation

Table 3: Printable ASCII Character to Hex Lookup

The American Standard Code for Information Interchange (ASCII) maps numerical byte values to text glyphs. Programmers inspecting hex dumps or debugging network protocols rely on this exact byte-to-glyph mapping:

Hex Byte Decimal Glyph Description Hex Byte Decimal Glyph Description
0x2032[SP]Space0x4165AUppercase Latin A
0x2133!Exclamation point0x4266BUppercase Latin B
0x2234"Double quotation0x4367CUppercase Latin C
0x2335#Number sign (Hash)0x4D77MUppercase Latin M
0x2436$Dollar sign0x5A90ZUppercase Latin Z
0x2840(Left parenthesis0x6197aLowercase Latin a
0x2941)Right parenthesis0x6298bLowercase Latin b
0x2A42*Asterisk0x6399cLowercase Latin c
0x2B43+Plus sign0x7A122zLowercase Latin z
0x2D45-Hyphen / Minus0x7B123{Left curly brace
0x2F47/Forward slash0x7D125}Right curly brace
0x30480Digit zero0x7E126~Tilde
0x39579Digit nine0x0A10\nLine Feed
0x3D61=Equals sign0x0D13\rCarriage Return

Table 4: Hex Opacity & Alpha Transparency Matrix (0% to 100%)

Modern CSS Color Module Level 4 supports 8-digit hexadecimal colors (#RRGGBBAA), where the final two characters specify alpha channel opacity. In 8-digit notation, 00 corresponds to 0% opacity (completely transparent) and FF corresponds to 100% opacity (fully opaque).

The mathematical conversion from an integer percentage P (0 to 100) to its 2-digit hexadecimal suffix is:

Alpha_Hex = round( (P / 100) * 255 ).toString(16).padStart(2, '0').toUpperCase()

Below is the reference cheat sheet for standard opacity steps, illustrated using brand blue (#2563EB):

Opacity Percentage Alpha Hex Suffix (AA) Decimal Value (0-255) 8-Digit CSS Example CSS RGBA Equivalent
100%FF255#2563EBFFrgba(37, 99, 235, 1.0)
95%F2242#2563EBF2rgba(37, 99, 235, 0.95)
90%E6230#2563EBE6rgba(37, 99, 235, 0.90)
85%D9217#2563EBD9rgba(37, 99, 235, 0.85)
80%CC204#2563EBCCrgba(37, 99, 235, 0.80)
75%BF191#2563EBBFrgba(37, 99, 235, 0.75)
70%B3179#2563EBB3rgba(37, 99, 235, 0.70)
65%A6166#2563EBA6rgba(37, 99, 235, 0.65)
60%99153#2563EB99rgba(37, 99, 235, 0.60)
55%8C140#2563EB8Crgba(37, 99, 235, 0.55)
50%80128#2563EB80rgba(37, 99, 235, 0.50)
45%73115#2563EB73rgba(37, 99, 235, 0.45)
40%66102#2563EB66rgba(37, 99, 235, 0.40)
35%5989#2563EB59rgba(37, 99, 235, 0.35)
30%4D77#2563EB4Drgba(37, 99, 235, 0.30)
25%4064#2563EB40rgba(37, 99, 235, 0.25)
20%3351#2563EB33rgba(37, 99, 235, 0.20)
15%2638#2563EB26rgba(37, 99, 235, 0.15)
10%1A26#2563EB1Argba(37, 99, 235, 0.10)
5%0D13#2563EB0Drgba(37, 99, 235, 0.05)
0%000#2563EB00rgba(37, 99, 235, 0.0)
Architecture Gotcha: CSS vs Android XML Byte Ordering
In standard web CSS, the alpha channel is placed at the end: #RRGGBBAA. In contrast, native Android XML color resources historically placed the alpha channel at the front: #AARRGGBB. A developer copying #802563EB into CSS will render an opaque color with a dark red channel, rather than a 50% translucent blue. Always verify the byte order specification of your runtime platform.

The 3-Digit and 4-Digit Shorthand Duplication Rules

CSS allows shorthand 3-digit hex notation (#RGB) and 4-digit notation with alpha (#RGBA). A widespread misconception among beginners is that shorthand appends trailing zeros (for example, assuming #F00 becomes #F00000). In reality, the CSS specification dictates that each character duplicates into a pair:

#RGB   ──►  #RRGGBB        (e.g., #FA0   ──►  #FFAA00)
#RGBA  ──►  #RRGGBBAA      (e.g., #38CF  ──►  #3388CCFF)

Because each character is cloned, shorthand can only represent colors where both nibbles of every channel are identical. For example, #123456 cannot be compressed into shorthand because 1 != 2, 3 != 4, and 5 != 6. Out of 16,777,216 standard TrueColor values, exactly 4,096 colors (163) can be expressed in 3-digit shorthand.

Table 5: Standard CSS Named Colors & The 'DarkGray' Paradox

The W3C CSS specification standardizes 140 named color keywords originating from the X11 Unix windowing system and HTML 3.2. Below is an engineering reference for popular named colors across primary hues:

Color Name Hex Notation RGB Value Perceived Luminance Color Family
crimson#DC143Crgb(220, 20, 60)0.160Red
red#FF0000rgb(255, 0, 0)0.213Red
coral#FF7F50rgb(255, 127, 80)0.370Orange
darkorange#FF8C00rgb(255, 140, 0)0.395Orange
gold#FFD700rgb(255, 215, 0)0.699Yellow
yellow#FFFF00rgb(255, 255, 0)0.928Yellow
lime#00FF00rgb(0, 255, 0)0.715Green
forestgreen#228B22rgb(34, 139, 34)0.189Green
cyan / aqua#00FFFFrgb(0, 255, 255)0.787Cyan
dodgerblue#1E90FFrgb(30, 144, 255)0.274Blue
blue#0000FFrgb(0, 0, 255)0.072Blue
rebeccapurple#663399rgb(102, 51, 153)0.076Purple
magenta / fuchsia#FF00FFrgb(255, 0, 255)0.285Purple
silver#C0C0C0rgb(192, 192, 192)0.527Gray
darkgray#A9A9A9rgb(169, 169, 169)0.396Gray
gray#808080rgb(128, 128, 128)0.216Gray
darkslategray#2F4F4Frgb(47, 79, 79)0.067Gray
black#000000rgb(0, 0, 0)0.000Neutral

The Historical 'DarkGray' Paradox: Notice in the table above that darkgray (#A9A9A9, luminance 0.396) is objectively lighter than standard gray (#808080, luminance 0.216). This occurred when the W3C combined HTML 3.2 with the X11 color registry. HTML 3.2 had specified gray as exact middle 50% gray (#808080). X11 had independently defined darkgray as #A9A9A9 (roughly 66% gray). When both catalogs merged into modern CSS, darkgray was preserved as lighter than gray.

Bitwise Manipulation: Unpacking Hex Colors Across Languages

In low-level graphics engines, canvas pipelines, and high-performance game loops, colors are packed into single 32-bit integers. Software engineers unpack individual 8-bit color channels using bitwise right shifts (>>) and bitwise AND masks (& 0xFF):

1. TypeScript / JavaScript

export interface RGBColor {
  r: number;
  g: number;
  b: number;
}

export function unpackHex(hexString: string): RGBColor {
  const cleanHex = hexString.replace(/^#/, '');
  const colorInt = parseInt(cleanHex, 16);
  if (isNaN(colorInt)) {
    throw new TypeError(`Invalid hexadecimal color format: ${hexString}`);
  }

  // Bitwise masking and shifting operations
  const r = (colorInt >> 16) & 0xff; // Shift 16 bits, mask low byte
  const g = (colorInt >> 8) & 0xff;  // Shift 8 bits, mask low byte
  const b = colorInt & 0xff;         // Mask low byte directly

  return { r, g, b };
}

2. Rust

#[derive(Debug, PartialEq, Eq)]
pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

pub fn unpack_hex(hex: u32) -> Rgb {
    Rgb {
        r: ((hex >> 16) & 0xFF) as u8,
        g: ((hex >> 8) & 0xFF) as u8,
        b: (hex & 0xFF) as u8,
    }
}

3. C / C++

#include <stdint.h>

typedef struct {
    uint8_t r;
    uint8_t g;
    uint8_t b;
} RGBColor;

RGBColor unpack_hex(uint32_t colorInt) {
    RGBColor c;
    c.r = (colorInt >> 16) & 0xFF;
    c.g = (colorInt >> 8) & 0xFF;
    c.b = colorInt & 0xFF;
    return c;
}

Color Science: From CRT Monitors to Relative Luminance and OKLCH

For three decades, web styling relied on hexadecimal color notations: #ffffff, #000000, and #3b82f6. Hex notation represented red, green, and blue photon intensities as three pairs of base-16 integers (00 to FF). It mirrored how 1990s cathode-ray tube (CRT) displays mapped electron guns to physical phosphors.

In modern web engineering, hex codes introduce significant architectural constraints. They confine rendering engines to the narrow, legacy sRGB color space, discarding roughly 30% of the richer saturation visible on Display P3 screens, iPhone OLED displays, and MacBook liquid retina panels.

Furthermore, neither hex nor HSL possesses perceptual uniformity. In HSL, pure blue (hsl(240, 100%, 50%)) and pure yellow (hsl(60, 100%, 50%)) share an identical lightness value of 50%. Human vision registers yellow as bright and blue as dark. CSS Color Module Level 4 resolved this by introducing OKLCH.

The Modern Color Space Comparison

Color Format CSS Syntax Example Supported Gamut Perceptually Uniform? Recommended Engineering Use
Hexadecimal #3b82f6 sRGB only No Static legacy fallbacks, build configs
RGB / RGBA rgb(59 130 246 / 0.8) sRGB only No Canvas 2D pixel manipulation
HSL hsl(217 91% 60%) sRGB only No (Distorts luminance) Legacy CSS preprocessors
OKLCH oklch(0.62 0.19 250) Wide Gamut (P3, Rec.2020) Yes (Calibrated to human vision) Design systems, dark modes, token scales

The Architecture of OKLCH

OKLCH models light through mathematical coordinates based directly on human retinal response:

oklch( Lightness  Chroma  Hue ) 
         │          │       │
         │          │       └── Hue Angle (0 to 360 degrees)
         │          │           0 = Red, 90 = Yellow, 140 = Green, 240 = Blue
         │          │
         │          └── Chroma / Saturation (0.0 to ~0.37+)
         │              0 = Pure Grayscale, 0.3+ = Ultra-vivid P3 Display Color
         │
         └── Perceived Lightness (0% = Pure Black, 100% = Pure White)
             Identical number = Identical perceived brightness across ALL hues!

Because lightness is perceptually calibrated, oklch(0.7 0.15 60) (yellow) and oklch(0.7 0.15 250) (blue) have identical perceived luminance. If dark text satisfies contrast ratios on the yellow variant, it is mathematically guaranteed to satisfy contrast ratios on the blue variant.

Automated Palette Generation in TypeScript

Here is an automated palette generator that converts a single brand hue into an accessible, 11-step CSS variable scale:

// src/styles/theme-engine.ts
export interface PaletteStep {
  token: string;
  lightness: number;
  chroma: number;
}

const PALETTE_CURVE: PaletteStep[] = [
  { token: '50',  lightness: 0.97, chroma: 0.02 },
  { token: '100', lightness: 0.93, chroma: 0.04 },
  { token: '200', lightness: 0.86, chroma: 0.08 },
  { token: '300', lightness: 0.77, chroma: 0.12 },
  { token: '400', lightness: 0.68, chroma: 0.16 },
  { token: '500', lightness: 0.58, chroma: 0.20 }, // Primary Brand Shade
  { token: '600', lightness: 0.50, chroma: 0.18 },
  { token: '700', lightness: 0.42, chroma: 0.15 },
  { token: '800', lightness: 0.33, chroma: 0.12 },
  { token: '900', lightness: 0.24, chroma: 0.08 },
  { token: '950', lightness: 0.15, chroma: 0.04 },
];

export function buildOklchScale(hueDegrees: number, prefix = 'brand'): Record {
  const tokens: Record = {};
  for (const step of PALETTE_CURVE) {
    tokens[`--color-${prefix}-${step.token}`] = `oklch(${step.lightness} ${step.chroma} ${hueDegrees})`;
  }
  return tokens;
}

Progressive Enhancement in Modern CSS

Deploy wide-gamut OKLCH colors while providing reliable sRGB hex fallbacks for older browsers:

/* styles/buttons.css */
:root {
  /* sRGB fallback */
  --accent: #2563eb;
}

@supports (color: oklch(0.6 0.2 250)) {
  :root {
    /* Wide-gamut P3 color on modern displays */
    --accent: oklch(0.58 0.22 255);
  }
}

.btn-primary {
  background-color: var(--accent);
  color: #ffffff;
  padding: 0.75rem 1.5rem;
  border-radius: 6px;
  font-weight: 600;
}

Looking for Structured Frontend Training?

Take your frontend engineering skills to the next level with hands-on projects, component architecture, and production best practices. Build real-world applications with our free structured courses:

Explore the Free Angular & Frontend Development Course →

Engineering Conclusion

Hexadecimal notation remains the universal bridge between raw binary machine data and human developers. Whether you are parsing network packets, executing bitwise bit shifts, or defining modern wide-gamut CSS variables, understanding hex fundamentals is an irreplaceable computer science skill.

Found this useful?
View all articles
Free Technical Interview Prep

Practicing for Engineering Interviews?

Skip the expensive coaching bootcamps and dry LeetCode memorization. Practice real production scenarios with instant turn-by-turn AI feedback on Frontend, Backend, System Design, and DSA.

Free Utilities

Recommended Developer Tools for this Topic

Explore all 25+ tools→

Keep Reading

Related Articles

Learn with Dropout Developer

Build real software with AI

Step-by-step learning paths, vibe coding tutorials, and certified developer programs designed for the modern engineer.