·7 min read

What is Hexadecimal? A Developer's Guide to Hex

Hexadecimal shows up everywhere in programming - from color codes to memory addresses to MAC addresses. Here's everything you need to know about the base-16 number system.

What is Hexadecimal?

Hexadecimal, often shortened to hex, is a base-16 number system. Decimal uses 10 digits, 0-9, while hexadecimal uses 16 symbols: 0-9 plus A-F.

The word comes from Greek hexa, meaning six, and Latin decem, meaning ten: literally six-ten, or sixteen.

In programming, hex values are often prefixed with 0x. For example, 0xFF is 255 in decimal.

Why Do We Use Hexadecimal?

Computers operate in binary, but binary numbers get long quickly. The decimal value 255 is 11111111 in binary, but simply FF in hex.

Hex works well because 16 is a power of 2. Each hex digit maps exactly to 4 binary digits, and two hex digits represent one byte.

  • Each hex digit maps exactly to 4 binary digits, called a nibble
  • Two hex digits represent exactly 1 byte, or 8 bits
  • Hex to binary conversion is simple: group bits by 4
  • Hex is more compact and readable than raw binary

The 16 Hex Digits

Here is the complete hex digit table with decimal and binary equivalents:

HexDecimalBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
A101010
B111011
C121100
D131101
E141110
F151111

A-F are case-insensitive. 0xff, 0xFF, and 0XFF represent the same value.

How to Read Hex Numbers

Hex works like decimal, except each position is a power of 16 instead of 10:

Decimal place values:  ... 1000  100  10   1
                           10^3  10^2  10^1 10^0

Hex place values:      ... 4096  256  16   1
                           16^3  16^2  16^1 16^0

Example: 0x2F4
  2 x 256  =  512
  F x 16   =  240    (F = 15, so 15 x 16)
  4 x 1    =    4
             -----
  Total    =  756 (decimal)

Converting Hex to Decimal

To convert hex to decimal, multiply each digit by its positional power of 16 and add the results:

Example: 0x1A3

  1 x 16^2 = 1 x 256 = 256
  A x 16^1 = 10 x 16 = 160
  3 x 16^0 = 3 x 1   =   3
                       -----
  Result:              419

So 0x1A3 = 419 in decimal.

Some common values worth memorizing:

  • 0xFF = 255, the maximum value of one byte
  • 0x100 = 256
  • 0xFFFF = 65,535, the maximum 16-bit unsigned value
  • 0x7FFFFFFF = 2,147,483,647, the maximum 32-bit signed integer

Converting Decimal to Hex

To convert decimal to hex, repeatedly divide by 16 and record the remainders:

Example: Convert 750 to hex

  750 / 16 = 46  remainder 14 (E)
   46 / 16 =  2  remainder 14 (E)
    2 / 16 =  0  remainder  2 (2)

Read remainders bottom-to-top: 2EE

So 750 = 0x2EE

Hex and Binary

The real beauty of hex is how cleanly it maps to binary. Each hex digit is exactly 4 bits, so conversion is just grouping:

Hex to Binary:
  0xA7 -> A = 1010, 7 = 0111 -> 10100111

Binary to Hex:
  11011110 -> 1101 = D, 1110 = E -> 0xDE

Byte example:
  0xFF -> 11111111 (all bits on, max byte value)
  0x00 -> 00000000 (all bits off, zero)

This is why hex is the preferred notation for viewing raw bytes in hex dumps, memory viewers, and debuggers.

Hex in the Real World

Hexadecimal appears throughout computing and development:

CSS/HTML Colors

Colors are represented as three bytes: red, green, and blue, written in hex:

ColorHex CodeRGB ValuesPreview
Red#FF0000255, 0, 0
Green#00FF000, 255, 0
Blue#0000FF0, 0, 255
White#FFFFFF255, 255, 255
Black#0000000, 0, 0

Other Common Uses

  • Memory addresses - debuggers show addresses like 0x7FFF5FBFF8A0
  • MAC addresses - network hardware IDs such as 00:1A:2B:3C:4D:5E
  • Unicode code points - characters identified as U+1F600
  • Error codes - Windows HRESULT codes like 0x80070005
  • Cryptographic hashes - SHA-256 and MD5 output is commonly shown in hex
  • IPv6 addresses - written in hexadecimal groups

Hex in Code

Every major programming language supports hex literals and conversion:

JavaScript

// Hex literals
const value = 0xFF; // 255

// Decimal to hex string
const hex = (255).toString(16); // "ff"

// Hex string to decimal
const dec = parseInt("ff", 16); // 255

// Pad to 2 digits (useful for colors)
const padded = (10).toString(16).padStart(2, "0"); // "0a"

Python

# Hex literals
value = 0xFF  # 255

# Decimal to hex string
hex_str = hex(255)        # '0xff'
formatted = f"{255:02x}"  # 'ff'

# Hex string to decimal
dec = int("ff", 16)  # 255

CSS

/* Full 6-digit hex */
color: #3B82F6;

/* Short 3-digit hex */
color: #FFF;  /* same as #FFFFFF */

/* 8-digit hex with alpha transparency */
color: #3B82F680;  /* 50% opacity */

Common Hex Mistakes

A few pitfalls to watch out for:

Confusing hex and decimal

10 in hex is 16 in decimal, not 10. Use 0x or # when context matters.

Forgetting to pad with zeros

A byte should be 2 hex digits. Write 0A, not just A, in byte-oriented formats.

Using letters beyond F

Hex only uses A-F. Letters like G, H, or Z are invalid hex digits.

Missing the # in CSS colors

CSS hex colors require the # prefix.

Convert Hex Values Instantly

Use our free Hex Converter tool to convert between hexadecimal, decimal, binary, and octal right in your browser.

Try Hex Converter

References

  1. Mozilla Developer Network. Hexadecimal - MDN Web Docs Glossary. https://developer.mozilla.org/en-US/docs/Glossary/Hexadecimal
  2. Mozilla Developer Network. <color> - CSS: Cascading Style Sheets. https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
  3. Mozilla Developer Network. parseInt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
  4. The Unicode Consortium. Unicode Code Charts. https://www.unicode.org/charts/
  5. IEEE. IEEE 802 - MAC Address Format. https://standards.ieee.org/products-programs/regauth/
USTHJP