CALCULATORCASTLE

Hex Calculator

Perform arithmetic on hexadecimal numbers and convert between hex, decimal, and binary.

About

Hex Calculator

Three calculators sit above this. The first adds, subtracts, multiplies or divides two hexadecimal values and reports the answer in both hex and decimal. The second turns a hex value into decimal and the third goes the other way. All three show the working, including the column-by-column carrying that makes hex arithmetic feel unfamiliar at first.

What hexadecimal is

Hexadecimal works exactly like decimal and binary, but on a base of 16 rather than 10 or 2. It needs sixteen digits, so after 0 to 9 it borrows the letters A to F for the values 10 to 15. Case does not matter: A and a are the same digit.

Its usefulness comes from the relationship with binary. Sixteen is two to the fourth power, so every hex digit stands for exactly four binary digits, a group known as a nibble. That makes conversion between the two mechanical rather than arithmetic: the binary value 1010101010 splits into nibbles as 10 1010 1010 and reads straight off as 2AA. A byte is always two hex digits, which is why hex is how computers show binary to people.

Hex, binary and decimal side by side

HexBinaryDecimal
000
111
2102
3113
41004
51015
61106
71117
810008
910019
A101010
B101111
C110012
D110113
E111014
F111115
141010020
3F11111163

The first sixteen rows are worth knowing by sight, because every hex operation eventually comes down to them.

Place values

Each column in a hex number is a power of 16, counting from zero on the right. In 2AA the right-hand A is the ones place, 160; the second A is 161, or sixteens; and the 2 is 162, or 256s. Remembering that A is 10 in decimal:

2AA=(2×162)+(A×161)+(A×160)=(2×256)+(10×16)+(10×1)=512+160+10=682

Converting from hex to decimal is always this: multiply each digit by its place value and add the results. Letter digits change nothing about the method.

1024=(1×163)+(0×162)+(2×161)+(4×160)=4132

Decimal to hex

Going the other way takes a little more work. One approach is repeated division by 16, keeping the remainders and reading them upwards, which is what the third calculator above shows. The other is to work down through the place values:

  1. Find the largest power of 16 that fits inside the number.
  2. Work out how many times it goes in, and note that count.
  3. Multiply the count by the power of 16 and subtract, leaving a remainder.
  4. Repeat with the remainder until 16 is larger than what is left; whatever remains is the 160 digit.
  5. Write each count in its place value, converting 10 to 15 into A to F.

Take 1500. The largest useful power is 162 = 256, which goes in 5 times: 5 × 256 = 1280, leaving 220. Sixteen goes into 220 thirteen times: 13 × 16 = 208, leaving 12. Since 16 is larger than 12, the last digit is 12. So 1500 = (5 × 162) + (13 × 161) + (12 × 160), and converting 13 to D and 12 to C gives 5DC.

Hex addition

Hex addition follows the same rules as decimal addition, with one difference: a column carries when it reaches 16, not 10.

 1 1
   8 A B
 +  B 7 8
 = 1 4 2 3

Reading right to left: B + 8 is 11 + 8 = 19 in decimal, and 19 is 13 in hex, being one 16 with 3 left over. Write the 3 and carry the 1. The next column is 1 + A + 7 = 1 + 10 + 7 = 18 decimal, which is 12 hex, so write 2 and carry again. The last column is 1 + 8 + B = 1 + 8 + 11 = 20 decimal, or 14 hex. The result is 1423.

Keeping the A to F values in mind while adding is the whole skill. Everything else is ordinary column arithmetic.

Hex subtraction

Subtraction works the same way with one thing to watch: a borrowed 1 is worth 16, not 10, because the column you borrow from is sixteen times larger than the one receiving it.

   5 D ¹C
 -  3 A F
 =  2 2 D

In the right-hand column C is 12 and F is 15, so 12 is too small and the column has to borrow. That reduces the D to C and lends 16, giving 16 + 12 − 15 = 13 decimal, which is D. The remaining columns need no borrowing: C − A is 12 − 10 = 2, and 5 − 3 = 2. The answer is 22D.

When the number being subtracted is the larger of the two, swap them, subtract, and put a minus sign in front. Reversing the example above to 3AF − 5DC gives −22D.

Hex multiplication

Multiplication is the fiddly one, because the intermediate products are larger and each needs converting. A multiplication table helps, and one is below.

     F A        3 × A = 1E, carry 1 to F
 ×   C 3        3 × F = 2D, + 1 = 2E
   2 E E      C × A = 78, carry 7 to F
 + B B 8 0    C × F = B4, + 7 = BB
 = B E 6 E

Each partial product is built the same way as in decimal, then the partials are added using hex addition. The result of FA × C3 is BE6E, which is 250 × 195 = 48,750 in decimal.

Hex division

Long division in hex is identical to long division in decimal, except that the multiplying and subtracting inside it happen in hex. The alternative is to convert to decimal, divide there, and convert back, which is what the calculator above does internally.

        D E F      12 × D = EA
   12 ) F A C E    FA − EA = 10
        E A        12 × E = 10C
        1 0 C      10C − FC = 10
          F C      12 × F = 10E
          1 0 E
          1 0 E
                0

Here FACE divided by 12 gives DEF with nothing left over: 64,206 ÷ 18 = 3,567 in decimal. Remember that any borrowing inside the subtraction steps is worth 16.

Hexadecimal multiplication table

×123456789ABCDEF10
1123456789ABCDEF10
22468ACE10121416181A1C1E20
3369CF1215181B1E2124272A2D30
448C1014181C2024282C3034383C40
55AF14191E23282D32373C41464B50
66C12181E242A30363C42484E545A60
77E151C232A31383F464D545B626970
88101820283038404850586068707880
99121B242D363F48515A636C757E8790
AA141E28323C46505A646E78828C96A0
BB16212C37424D58636E79848F9AA5B0
CC1824303C4854606C7884909CA8B4C0
DD1A2734414E5B6875828F9CA9B6C3D0
EE1C2A38465462707E8C9AA8B6C4D2E0
FF1E2D3C4B5A69788796A5B4C3D2E1F0
10102030405060708090A0B0C0D0E0F0100

Negative values and two's complement

The calculator above accepts a minus sign and reports a negative answer plainly, which is what you want for ordinary arithmetic. Computers do it differently, and the difference catches people out when reading a memory dump.

Inside a fixed-width register there is no room for a sign character, so negative numbers are stored using two's complement: the top bit carries the sign, and a negative value is written as the positive one subtracted from 2 raised to the width. In a single signed byte that makes FF equal to −1, FE equal to −2, and 80 the most negative value at −128, while 7F is the largest positive at 127.

So the same two hex digits mean 255 or −1 depending entirely on whether the byte is being read as signed or unsigned, and nothing in the digits themselves tells you which. A value of FFFFFFFF in a 32-bit register is either about 4.29 billion or simply −1. When a debugger shows an implausibly enormous number, an unsigned reading of a negative value is usually the reason.

Notation you will meet

The same value gets written several ways depending on where it appears. 0x1A3 is the C convention and the one most languages adopted. #1A3 means a colour in CSS and HTML. 1A3h is the assembler style, with a trailing h. Some documentation uses a subscript, as in 1A316. None of these change the number, and the calculators above accept a plain value with or without the 0x prefix.

Two habits are worth keeping. Case is irrelevant to the value but not to consistency, and mixed case such as 1a3F is legal and horrible to read; pick upper or lower and stay there. Leading zeros are also free, and hex normally keeps them so a value fills its width, since 0A and 0x0000000A carry the same value as A but say something useful about the size of the field holding it.

Hexadecimal displaced octal, base 8, for the same reason it displaced raw binary. Octal groups bits in threes, which suited machines with 12-, 24- and 36-bit words, but once the 8-bit byte became standard the threes stopped aligning with anything. Four bits divide a byte evenly and eight bits divide a 32-bit word evenly, so hex fits modern hardware and octal does not. Octal survives mainly in Unix file permissions, where three bits per group is exactly what is wanted.

Where hex actually turns up

Web and design colours are three bytes of hex, written #RRGGBB, so #FF0000 is full red and #AABBCC is the same value the second calculator would read as 11,189,196. The shorthand #ABC expands to #AABBCC.

Memory addresses and machine code are shown in hex because a byte is exactly two digits, so a dump lines up in neat columns. MAC addresses are six bytes in hex, and IPv6 addresses are eight groups of four hex digits, which is why they look so different from IPv4. Unicode code points are written U+ followed by hex, so U+1F600 is the grinning face. Hashes such as SHA-256 are quoted as 64 hex characters, which is exactly 256 bits at four bits per character.

The habit even shows up in jokes: words made only from A to F, such as DEADBEEF, CAFEBABE and FACE, are valid hex numbers, and programmers use them as recognisable marker values in memory.

Reading your result

Check that your input only uses 0-9 and A-F. A stray O for zero or an I for one is the usual cause of a rejected value, and the calculator will not guess which you meant.

The first calculator gives both forms of the answer, and the decimal line is the one to check against a spreadsheet or another tool. Division reports a quotient and a remainder rather than a fraction, since hex division here is whole-number division.

All three run in exact whole-number arithmetic, so a sixteen-digit hex value such as a memory address or a hash fragment converts without losing its last digits, which is not true of tools that convert through an ordinary decimal number.

Common questions

Frequently asked questions

Anywhere binary needs to be readable. One hex digit is exactly four binary digits and a byte is exactly two hex digits, so memory dumps, machine code, MAC addresses, IPv6 addresses, Unicode code points and colour codes such as #FF0000 are all written in hex.

Multiply each digit by its place value, which is a power of 16 counting from zero on the right, then add the results. For 2AA that is (2 x 256) + (10 x 16) + (10 x 1) = 512 + 160 + 10 = 682. Remember that A to F stand for 10 to 15.

Divide by 16 repeatedly, keep each remainder, and read the remainders from the bottom up. Alternatively work down the place values: 1500 holds five 256s, then thirteen 16s, then 12, giving 5, D and C, or 5DC.

Base 16 needs sixteen distinct digits, and the decimal system only supplies ten. The letters A to F fill the remaining six values, 10 through 15, because they were already available on every keyboard and are unambiguous next to the numerals.

The borrowed 1 is worth 16 rather than 10, because the column it comes from is sixteen times larger. So in a column where C, which is 12, must be taken from F, which is 15, you borrow to get 16 + 12 - 15 = 13, which is D.

Sixteen is two to the fourth, so each hex digit is exactly four binary digits, called a nibble. That means conversion needs no arithmetic: split the binary into groups of four from the right and read each group off the table. 1010101010 becomes 2AA.

No, the prefix is a programming convention that tells a compiler the number is base 16, and it is not part of the value. The calculators above accept a value with or without it, and letters can be typed in upper or lower case.

Yes. All three run in exact whole-number arithmetic rather than converting through an ordinary decimal number, so a long value such as a memory address or a hash fragment keeps every digit instead of losing precision at the end.