Hexadecimal Number System

Hexadecimal is a base-16 number system with the digits 0 to 9 and A to F. Each digit is four bits, so a byte is two digits and FF is 255. This page covers counting, conversion, notation in code, and where hex is used.

What is hexadecimal?

Hexadecimal is a positional number system with base 16. It uses sixteen digits: 0 to 9 for the values zero to nine and the letters A to F for ten to fifteen. Each position is worth sixteen times the one to its right, so the number 2F3 means 2 × 256 + 15 × 16 + 3 = 755. The word combines the Greek hex (six) with the Latin-derived decimal (tenth), and a purist would write sexadecimal, an older term that is almost never used.

Hexadecimal matters in computing because sixteen is a power of two. One hex digit holds exactly four bits, so a byte is two digits and a 32-bit number is eight. Binary is how hardware stores everything and is too long to read; hex is the same information compressed fourfold, with no arithmetic involved in the conversion.

The 16 hexadecimal digitsThe digits 0 to 9 stand for 0 to 9 and the letters A to F stand for 10 to 15. After F the next number is 10, which is sixteen.000000110001220010330011440100550101660110770111881000991001A101010B111011C121100D131101E141110F151111
After F the count carries into the next column: hex 10 is sixteen, hex 100 is two hundred fifty-six.

Counting in hexadecimal

Counting works as it does in decimal until the digits run out. After 9 comes A, then B, C, D, E and F, which is fifteen. The next number is 10, written one-zero and read as one-zero: it means one sixteen and no ones, so it equals sixteen. The table counts from 0 to 20 in hex with the equivalent decimal and the four- or five-bit binary.

Counting from 0 to 20 in hexadecimal
HexDecimalBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
A101010
B111011
C121100
D131101
E141110
F151111
101600010000
111700010001
121800010010
131900010011
142000010100
152100010101
162200010110
172300010111
182400011000
192500011001
1A2600011010
1B2700011011
1C2800011100
1D2900011101
1E3000011110
1F3100011111
203200100000

Round numbers fall in different places. 10 hex is sixteen, 20 hex is thirty-two, 100 hex is 256 and 1000 hex is 4,096. A number in hex with n digits is below 16^n: three digits cover up to FFF = 4,095. The hex digits in the first column are the same in upper and lower case, a1 and A1 being the same number, and the choice is style.

Hexadecimal and the byte

A byte is eight bits and has 256 possible values, 00 to FF. The two hex digits are the high nibble and the low nibble, each four bits, so 3A is the nibbles 3 (0011) and A (1010). That fixed width is why hex is written with leading zeros in data: 0A for ten with the leading zero kept, so every byte is two characters and a string of bytes can be cut into pairs without ambiguity.

Larger units are multiples of the byte. A 16-bit value is four hex digits, a 32-bit value eight, a 64-bit value sixteen. A 128-bit value such as a UUID or an MD5 digest is 32 digits. These counts are useful as sanity checks: a SHA-256 hash that is not 64 hex digits is truncated or not a SHA-256.

Half a byte has its own name, the nibble (sometimes spelled nybble), and it is the unit that a single hex digit counts. Programmers say high nibble and low nibble for the two digits of a byte, and file formats pack two small values, such as two 4-bit pixel colors, into one byte for that reason.

How to convert hexadecimal

Each target has a dedicated converter with the method written out. The short version of each is below, so the right page can be picked at once.

Converting hexadecimal
ToMethod in one lineConverter
DecimalMultiply each digit by its power of 16 and addHex to decimal
BinaryReplace each digit with its four bitsHex to binary
TextRead the digits as pairs of bytes and decode the bytesHex decoder
ASCIISame, restricted to values 00 to 7FHex to ASCII
Color valuesSplit into red, green, blue pairs and convert eachHex to RGB
Any baseEnter one value, see decimal, binary, octalHex converter
From decimalDivide by 16 repeatedly and read the remaindersDecimal to hex
From textEncode the characters to bytes and write each as two digitsText to hex

How hex is written in source code and data

There is no single way to say a number is hexadecimal. The prefix 0x comes from C and is the most widespread, but each language family has its own, and data formats often use none at all. The notation matters when copying values between systems, because a parser that expects one form can silently read another as decimal.

Ways of writing the number 255 in hexadecimal
NotationWritten asWhere
0x prefix0xFFC, C++, Java, JavaScript, Python, Go, Rust, shell arithmetic
h suffix0FFhIntel assembly (a leading 0 if the number starts with a letter)
$ prefix$FFPascal and Delphi, Motorola-style assembly
&H prefix&HFFVisual Basic
# prefix#FFCSS colors (#FF8800); not a general number
16# prefix16#FFBash arithmetic, Ada (16#FF#), Erlang
\x escape\xFFStrings in C, Python and many other languages
% escape%FFURLs (percent-encoding of a byte)
No markerFFHex dumps, hashes, MAC addresses, data files

Because of the variety, tools that accept hex have to accept several forms. The decoders on this site read 0x, \x, %, the h suffix and bare digits, and ignore separators. Pasting a value with a prefix the tool does not know is the most common reason a conversion reports an invalid character.

Where hexadecimal is used

Hex appears wherever people read raw data. The usage groups into a few families: identifiers made of bytes, addresses and offsets, characters, and colors.

Hexadecimal in everyday computing
UseExampleWhat the digits mean
Web colors#0B5E6BThree bytes: red 0B, green 5E, blue 6B
MAC addresses00:1A:2B:3C:4D:5ESix bytes, 48 bits, usually separated by colons or hyphens
IPv6 addresses2001:0db8:0000:0000:0000:ff00:0042:8329Eight groups of 16 bits, 128 bits in all
UUIDs123e4567-e89b-12d3-a456-42661417400032 hex digits in groups of 8-4-4-4-12, 128 bits
Hash digests64 digits for SHA-256256 bits, written as hex so it can be copied and compared
Memory addresses0x7FFE1A2B3C4DA pointer; the digits show alignment and region at a glance
Unicode code pointsU+1F600The number of a character; 1F600 is the grinning face emoji
Error codes0x80070005A Windows HRESULT: severity, facility and code packed into 32 bits
File signatures89 50 4E 47The first bytes of a PNG file; see the signature table on the ASCII page

The pattern behind all of these is the same: the data is bytes, hex is the shortest way to show bytes without losing alignment, and a person comparing two values can find the first differing digit by eye. The tools on this site convert between those forms and the text, decimal or binary the digits stand for. A full alphabetical list of the tools is on the tools page.

Hexadecimal in text and Unicode

Characters are numbers too, and the numbers are quoted in hex. Unicode names each code point U+ followed by at least four hex digits: U+0041 is A, U+00E9 is é, U+20AC is the euro sign and U+1F600 is an emoji. The code point is not the same as the bytes in a file. In UTF-8 the euro sign is the three bytes E2 82 AC, and in UTF-16 it is the single 16-bit unit 20AC, written 20 AC or AC 20 depending on byte order. Hex shows both, and keeping the two apart is half of reading encoded text.

Markup uses hex for characters as well. HTML numeric references take the form € for the code point, CSS escapes write \20AC in a string, and JavaScript and JSON write €. URLs use percent-encoding of the UTF-8 bytes (%E2%82%AC). The hex decoder turns byte pairs into text with the encoding of your choice, and text to hex goes the other way.

Reading a hex dump

A hex dump is the standard way to look at bytes, and the layout is nearly universal: an offset column, the bytes in hex, and a column of the printable characters. This is the output of od for a short string, with the offset in hex.

od -A x -t x1z output for the 14 bytes of Hello, World! and a line feed
OffsetBytes in hexText
00000048 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 0a>Hello, World!.<

The final 0a is the line feed, shown as a dot in the text column because it does not print. Offsets are in hex, so the second line of a 16-byte-per-row dump starts at 000010 and the third at 000020; to find byte number 100 of a file, 100 = 0x64, so look on the row for 000060 at the fifth position. The text column is only a convenience. When pasting a dump into a decoder, remove the offset and text columns first.

Hexadecimal in memory and debugging

Debuggers print addresses and register values in hex because they are made of bytes, and a person comparing two addresses wants to see which digits differ. Addresses on 64-bit systems are written with up to sixteen digits, but most programs use a small part of the space, so you see values such as 0x7FFE1A2B3C4D for stack addresses and shorter ones for code. The low digits reveal alignment: an address ending in 0 is 16-byte aligned, one ending in 8 is 8-byte aligned, and an odd address is not aligned for anything wider than a byte.

Byte order matters when a multi-byte value is read from memory. Most desktop processors store the least significant byte first (little-endian), so the 32-bit number 0x12345678 appears in a dump as 78 56 34 12. Network protocols and many file formats use the opposite order, called big-endian or network byte order, and write 12 34 56 78. The numbers are the same; the dump shows the order in which the bytes sit in memory. When a value looks wrong by a factor of 256, reversing the byte pairs is the first thing to try.

Hexadecimal color codes

CSS and most design tools write colors as hex: a # followed by two digits each for red, green and blue. Each pair is a byte from 00 to FF, the intensity of one light channel, so #FF0000 is full red and #000000 is black. The same notation comes in four lengths. The short forms double each digit, so #F80 means #FF8800. The forms with an alpha channel add a fourth digit or pair for opacity.

The four hex color forms in CSS
FormExampleExpands toMeaning
#RGB#F80#FF8800Each digit doubled: red FF, green 88, blue 00
#RGBA#F80A#FF8800AAThe same, with alpha AA (about 67% opaque)
#RRGGBB#FF8800#FF8800Three bytes: orange
#RRGGBBAA#FF880080#FF880080The same orange at 50% opacity (80 is 128 of 255)

The six-digit form covers 16,777,216 colors (256 to the third power). Converting a code to the red, green and blue numbers that design tools show is the job of hex to RGB, and the result of mixing is arithmetic that the hex calculator can do per channel.

Hexadecimal and Base64

Hex and Base64 both write bytes as printable characters, and the choice between them is a trade-off. Hex uses two characters per byte, so the text is twice the size of the data, but each byte stays separate and readable, which suits debugging and short values such as hashes. Base64 uses four characters for every three bytes, a third larger than the data, and suits long payloads such as images and keys in text formats, at the cost of bytes that no longer line up with the characters.

The same 4 bytes (DE AD BE EF) in both forms
EncodingOutputCharactersOverhead
HexadecimalDEADBEEF8100%
Base643q2+7w==8100% here, 33% on long data

For four bytes the two come out the same length because Base64 pads the last group. On long data the difference shows: a megabyte becomes 2 MB as hex and about 1.33 MB as Base64. When the data has to be compared by eye, or each byte matters, hex is better, and when size matters Base64 is. The conversion between them goes through bytes, not through numbers, as the Base64 reference explains.

Arithmetic in hexadecimal

Arithmetic follows the decimal rules with a different carry point. Addition carries when a column reaches 16, subtraction borrows 16, and multiplication uses a table in which 6 × 7 is 2A. Tables, worked examples and bitwise operations are on the hex calculator. Bitwise operations deserve a mention here because they are the reason hex is preferred over decimal in low-level code: AND with 0F keeps one digit and AND with F0 keeps the other, which has no counterpart in decimal.

Hex lengths of hashes and identifiers

The number of hex digits in an identifier gives away its size in bits, and it is the quickest way to tell what kind of value a string is. Each digit is four bits, so a digest of n bits is n / 4 digits long. A string that has the wrong number of digits for the algorithm it claims has been truncated, padded or mislabeled.

Common hex strings and their lengths
ValueBitsBytesHex digits
CRC-32 checksum3248
MD5 digest1281632
UUID (without hyphens)1281632
SHA-1 digest1602040
SHA-256 digest2563264
SHA-512 digest51264128
MAC address (without separators)48612
AES-256 key2563264

Hex is the usual written form of these values because it is compact, case-insensitive for comparison, and safe in every text format. MD5 and SHA-1 are no longer secure against deliberate collisions, but their lengths are still a reliable way to recognize old checksums and Git object names, which are SHA-1 digests of 40 hex digits.

Hexadecimal in machine code and floating point

Programs are bytes, and disassemblers show them in hex next to the instruction text. On x86, the single byte 90 is NOP (do nothing), C3 is RET (return from a function) and CC is INT3, the breakpoint instruction that debuggers insert. A function that ends in C3 and is padded with CC bytes between functions is a common sight in a dump of compiled code, and the pattern is the fastest way to find function boundaries by eye.

Floating-point numbers also have a hex form, since the IEEE 754 format is a bit pattern. The 32-bit float 1.0 is 3F800000 and −2.0 is C0000000; the high digits hold the sign and exponent, and the low digits the fraction. The same four bytes read as an integer would be 1,065,353,216 for 3F800000. The sign, exponent and fraction fields are explained on the hex to float converter. Writing a decimal number as its bytes is the job of float to hex.

Writing hex clearly: case, grouping and prefixes

Hex has no official style, but some conventions prevent mistakes. Pick one case for a document and keep it. Standards differ: RFC 4648 writes Base16 in upper case, while the UUID specification asks for lower-case output and accepts either case on input. Group long values for the reader, with a space or underscore every four digits or every byte, and never in the middle of a byte.

Mark the base whenever a number could be read as decimal. 10 is ten in decimal and sixteen in hex, and 11 is eleven or seventeen. In prose write 0x10 or 10h; in a table of hex values, state the base in the header. Keep the leading zeros of fixed-width fields (0x007F for a 16-bit value) so the width is visible, and do not mix hex and decimal in the same list without marking each.

Reading hex fluently

A few habits make hex quick to read. Memorize the values of A to F (10 to 15) and the powers 16, 256, 4,096 and 65,536. Learn the four-bit groups for the digits you meet most (1 is 0001, 8 is 1000, F is 1111). Recognize the common bytes: 00 and FF for none and all bits set, 20 for the space, 0A and 0D for line breaks, 30 to 39 for digits, 41 to 5A for capitals and 61 to 7A for lower case. With that vocabulary a dump of text is legible without a tool, and anything that is not text stands out by being outside those ranges.

For practice, take a short string you know and convert it to hex, then cover it up and convert back. The tools on this site do the conversion and also check the working: the ASCII table lists all 128 codes with hex, decimal, octal and binary, and the hex to ASCII converter reads any pairs you type.

Words and jokes in hex

Hex digits include enough letters to spell words, and programmers use the fact to make values easy to recognize in a debugger. The practice is called hexspeak. Real examples with defined meanings are common: the Java class file format begins with the four bytes CAFEBABE, and the Mach-O object format used on macOS starts with FEEDFACE in its 32-bit form. Microsoft's debug heap fills uninitialized memory with BAADF00D, and DEADBEEF is a conventional marker that is easy to spot in a dump.

Hex words and their decimal values
HexDecimalWhere
CAFEBABE3,405,691,582Magic number at the start of every Java class file
DEADBEEF3,735,928,559A recognizable filler value used in debugging
BAADF00D3,131,961,357Marks uninitialized heap memory in Microsoft debug builds
FEEDFACE4,277,009,102Magic number of 32-bit Mach-O files
C0FFEE12,648,430A popular choice for color and constant names

Why base 16: a short history

Computers store bits, and people need a shorthand. Octal, three bits per digit, was the first widely used one, and fits machines with word lengths divisible by three. When 8-bit bytes became the unit of storage, two hex digits per byte fit better than three octal digits (which would leave one bit over). IBM's System/360, introduced in 1964, used byte addressing and hexadecimal in its documentation, and later systems followed. Early notations disagreed on which symbols to use for the six extra digits, and the letters A to F won out.

The related encoding Base16 is defined in RFC 4648, which also defines Base32 and Base64. It writes each byte as two upper-case hex digits, which is the form that hex dumps, hash listings and the converters on this site produce and accept. The same RFC's test vector for it is foobar → 666F6F626172.

Reading sizes and offsets: why 0x1000 is 4 KiB

Hex makes the sizes of memory and files readable because they are powers of two, and a power of two in hex is a 1 followed by zeros. Each zero is a factor of 16, so 0x100 is 256, 0x1000 is 4,096 and 0x100000 is 1,048,576. Anyone who reads a memory map learns to see 0x1000 as 4 KiB, the size of a typical memory page, 0x10000 as 64 KiB, and 0x100000 as 1 MiB, without any arithmetic. The number of zeros tells the power: three zeros is 4,096, which is 2 to the 12th power, because each zero adds four bits.

Powers of two written in hex
HexBits (power of 2)DecimalSize
0x400101,0241 KiB
0x1000124,0964 KiB, a common memory page
0x100001665,53664 KiB, the range of a 16-bit value
0x100000201,048,5761 MiB
0x10000002416,777,21616 MiB, the number of 24-bit colors
0x100000000324,294,967,2964 GiB, the range of a 32-bit value

The same habit works for offsets inside a file. If a header says a section starts at 0x2000 and has a length of 0x400, it ends at 0x23FF, because the last byte is one before the next boundary, and the whole range is 8,192 to 9,215 in decimal. Adding and subtracting such values is one reason programmers prefer hex for addresses; the hex calculator does the arithmetic and shows both bases.

One caution: a round number in hex is not the same as a round number in decimal. A file of 0x10000 bytes is 65,536 bytes, which a disk utility reports as 64 KiB, while a vendor that counts in thousands would call it 65.5 KB. When a size in hex and a size in a file manager disagree by a few percent, the mix of binary and decimal units is the usual reason, and not an error in either number.

Reading MAC and IPv6 addresses digit by digit

A MAC address is six bytes, written as twelve hex digits. The first three bytes are an organizationally unique identifier assigned by the IEEE to the manufacturer, and the last three are the device's own number. The first byte also carries two flags in its lowest bits. The lowest bit says whether the address is for one device (0) or a group (1), and the next bit says whether the address is globally assigned (0) or set locally, as virtual machines and privacy features do (1). That is why a MAC that starts with 02 or 0A is usually not a factory address, and one that starts with 01 is a multicast address.

The two low bits of the first byte of a MAC address
First byteBitsGroup bitLocal bitMeaning
000000000000unicast, globally assigned
010000000110multicast, globally assigned
020000001001unicast, locally administered
030000001111multicast, locally administered

An IPv6 address is eight groups of four hex digits, each group 16 bits. To write it shorter, drop the leading zeros of each group and replace the longest run of two or more all-zero groups with a double colon, once. The address 2001:0db8:0000:0000:0000:ff00:0042:8329 becomes 2001:db8::ff00:42:8329 (see RFC 5952 for the recommended text form). Expanding it back is the reverse: count the groups that are present, and the double colon stands for as many zero groups as are missing to reach eight.

An IPv4 address inside IPv6 shows why hex suits bytes. The IPv4-mapped address for 192.168.0.1 is ::ffff:c0a8:1, which is the bytes C0 A8 00 01 regrouped as the two 16-bit groups c0a8 and 0001 after the ffff marker.

Decoding a Windows error code such as 0x80070005

Windows reports many failures as an HRESULT, a 32-bit number shown in hex. The bits are packed: the top bit is the severity (1 for failure), bit 29 marks a customer-defined code, bits 16 to 26 hold the facility that produced the error, and the low 16 bits are the code itself. Reading the hex digit by digit is faster than searching for it: the leading 8 means failure, the 007 in the middle is facility 7, which stands for Win32 errors, and the 0005 is Win32 error 5, access denied. The code 0x80070005 is listed in Microsoft's common HRESULT values as E_ACCESSDENIED.

Splitting HRESULT values into their fields
CodeSeverityCustomer bitFacilityCode fieldName
0x800700051070x0005E_ACCESSDENIED
0x800040051000x4005E_FAIL

A different family, NTSTATUS, has a similar layout with two severity bits. The value 0xC0000005, which crash dialogs show as an access violation, starts with C, the two top bits 11 for an error. The point is the same in both: an error code in hex is a record of fields, and the first digit alone often tells you whether you are looking at a success, a warning or a failure. If the converter gives you the digits of a code you found in a log, the hex to binary page shows how to lay out the fields.

Hex in spreadsheets and calculators

Excel and Google Sheets both have hex functions: HEX2DEC, DEC2HEX, HEX2BIN, BIN2HEX and others. They have limits worth knowing. According to Microsoft's documentation, HEX2DEC accepts at most 10 hex characters, which is 40 bits, with the top bit as the sign. A 64-bit identifier or a SHA-256 digest does not fit. DEC2HEX works between −549,755,813,888 and 549,755,813,887, the 40-bit signed range, and returns an error outside it.

There are also the hazards of cell types. A hex string such as 1E5 is read as the number 100,000 in scientific notation unless the cell is formatted as text, and a leading zero (0A) is dropped when the value is stored as a number. Both are common reasons why a hex value pasted from a spreadsheet no longer matches the one in the source. For anything beyond 40 bits, convert outside the spreadsheet with hex to decimal and decimal to hex, which use arbitrary-precision integers. Windows Calculator in Programmer mode and the hex calculator on this site handle the arithmetic side; the hex calculator shows the result in decimal and binary at the same time.

Questions about hexadecimal

Is hexadecimal the same as base 16?

Yes. Hexadecimal is the name for the base-16 positional number system. Base16 is also the name of an encoding in RFC 4648 that writes bytes as pairs of hex digits, which is the form hex decoders read.

Why does hexadecimal use letters?

A base-16 system needs sixteen different digit symbols, and the ten decimal digits supply only ten. The letters A to F were chosen for the six remaining values, 10 to 15, so every position of a hex number is a single character.

How many bits is one hex digit?

Four. A hex digit has 16 possible values, which is 2 to the 4th power. Two hex digits make eight bits, a byte, and the range 00 to FF covers all 256 byte values.

Is hex case-sensitive?

The value is not: ff and FF are the same number. Some tools and style guides prefer one case, and string comparison is case-sensitive, so normalize the case before comparing two hex strings as text.

What does the 0x in front of a hex number mean?

It marks the number as hexadecimal. The prefix comes from the C language and was copied by C++, Java, JavaScript, Python and many others. It is not part of the value: 0xFF and FF are both 255.