Number System Conversion: Binary, Octal, Decimal and Hexadecimal

Number system conversion rewrites the same value in a different base: 45 in decimal is 101101 in binary, 55 in octal and 2D in hex. The value does not change; only the digits and their place values do.

This page explains what a base is, how position gives each digit its weight, and the two methods that convert any base to any other.

It covers numbers, not character codes. For the codes behind letters, binary code explained from the alphabet up takes this further.

What is number system conversion?

Converting a number between bases changes how you write a value, not what the value is worth. A base is the count of distinct digits a system uses, and it sets what each position is worth. Change the base and the digits change with it, while the quantity underneath them holds still.

A price tag reading 45 and a switch pattern reading 101101 describe one quantity in two notations, in the way a distance is the same whether you write it in miles or kilometres. Conversion is a translation between notations, so a converter that altered the value would break the only rule the operation has.

One value in four bases, from the same digits: 101101 in binary is 45 in decimal, 0o55 in octal and 0x2D in hex. Read that row in either direction and the arithmetic is the same.

10011100

The same conversion code writes every worked example on this page.

What are the binary, octal, decimal and hexadecimal number systems?

Binary, octal, decimal and hexadecimal are four positional number systems, using 2, 8, 10 and 16 digits. Each of them writes a value as digits whose place values are powers of its own base, and only the base itself differs.

Binary is the machine's own notation, octal packs three bits into a digit, and hexadecimal packs four. Each profile below gives the same facts in the same order.

Binary (base 2)

Binary is base 2, the system a circuit can hold directly. Its digits are 0, 1 and nothing else, so each position is worth twice the one to its right: position n is worth 2^n, which gives the low weights 16, 8, 4, 2, 1. A binary value carries the prefix 0b in prose, as in 0b1010, or a subscript on paper, as in 1010₂.

Width comes from the hardware rather than the value. Eight bits make a byte and cover 0–255; four bits make a nibble and cover 0–15. You meet binary in logic gates, registers, subnet masks, bit flags.

Why do computers use binary?

Two states are the only thing a circuit can hold reliably.

A wire that sits either high or low survives noise, voltage drift and the spread between one manufactured part and the next, because the gap between the two states is wide. Ten voltage levels would have to be told apart on every gate, at every temperature, for the life of the part. Binary trades digit count for certainty: values get longer, and the hardware reading them stops guessing.

Octal (base 8)

Octal is base 8, and its digits run 0–7. Position n is worth 8^n, so the low weights are 4096, 512, 64, 8, 1. An octal value carries the prefix 0o in prose, as in 0o17, and C and several shells write a leading zero instead, as in 017.

Its widths follow from that: 3 digits cover a byte (000–377); Unix permissions 000–777. You meet octal in chmod 755, file modes, escape sequences. In chmod 755 each digit carries the read, write and execute bits for one class of user, three bits at a time.

Decimal (base 10)

Decimal is base 10, with digits 0–9 and position n worth 10^n, giving the weights 10000, 1000, 100, 10, 1. It needs no prefix, because it is what a reader assumes; a page that has to be explicit writes a subscript, as in 17₁₀. It has no fixed width; a page working in bytes treats 0–255 as its range.

Decimal is also the odd one out. Ten is not a power of two, so the bits per decimal digit come out not integral (~3.32). That number is why decimal has no shortcut into binary. You meet decimal in everyday arithmetic, ASCII code lists.

Hexadecimal (base 16)

Hexadecimal is base 16. Its digit set is 0–9, A–F (A=10 … F=15; case-insensitive), which is where the letters come from. Position n is worth 16^n, so the low weights are 65536, 4096, 256, 16, 1. A hex value carries the prefix 0x in prose, as in 0x1F, and a leading # when it names a colour.

Hex is built around the byte: 2 digits cover a byte (00–FF); 6 digits = RGB colour; 8 digits = 32-bit word. You meet it in memory addresses, colour codes, MAC addresses, hex dumps, error codes. Two digits per byte, with no digit ever straddling a byte boundary, is what makes hexadecimal the usual shorthand for a run of bits.

What digits does each number system use?

A base with radix b uses exactly b digits, running from 0 to b − 1.

Binary has 2, octal has 8, decimal has 10 and hexadecimal has 16: 0–9, A–F (A=10 … F=15; case-insensitive). Those six letters are digits, not labels — A is worth ten in the way 9 is worth nine, and it takes its place value the same way. A digit above the set is not bad arithmetic but bad reading: 8 cannot appear in an octal value, and 2 cannot appear in a binary one. The table gives each system's digits, its largest digit and how many distinct digits it holds.

Each number system: its digits, place values, typical range and typical use
Number systemDigitsLargest digitDistinct digitsBits per digitPlace valuePlace valuesTypical rangeTypical usePower of two
Binary (base 2)0, 11212^n16, 8, 4, 2, 18-bit byte 0–255 (00000000–11111111); 4-bit nibble 0–15; 16/32/64-bit wordslogic gates, registers, subnet masks, bit flagsyes
Octal (base 8)0–77838^n4096, 512, 64, 8, 13 digits cover a byte (000–377); Unix permissions 000–777chmod 755, file modes, escape sequencesyes
Decimal (base 10)0–9910not integral (~3.32)10^n10000, 1000, 100, 10, 1any; decimal pages treat 0–255 as the byte rangeeveryday arithmetic, ASCII code listsno
Hexadecimal (base 16)0–9, A–F (A=10 … F=15; case-insensitive)1516416^n65536, 4096, 256, 16, 12 digits cover a byte (00–FF); 6 digits = RGB colour; 8 digits = 32-bit wordmemory addresses, colour codes, MAC addresses, hex dumps, error codesyes

What is positional weight (place value) in a number system?

Positional weight is what a digit is worth because of where it sits.

The rule is one line: a digit contributes digit × base^position, with position counted from 0 at the right-hand end. In decimal the low weights run 10000, 1000, 100, 10, 1; in binary 16, 8, 4, 2, 1; in hex 65536, 4096, 256, 16, 1. Nothing else in a conversion is new. Both universal methods below apply that one rule, one of them forwards and one backwards.

Write the place values above the digits and the arithmetic reads itself. Take the binary value 101101, whose place values run 32, 16, 8, 4, 2, 1 from the left:

32168421
101101

Result: 101101 = 45

Powers of two, 2⁰ to 2¹⁶
n2ⁿValueMax at n bits
02⁰10
121
243
387
42⁴1615
52⁵3231
62⁶6463
72⁷128127
82⁸256255
92⁹512511
102¹⁰10241023
112¹¹20482047
122¹²40964095
132¹³81928191
142¹⁴1638416383
152¹⁵3276832767
162¹⁶6553665535

Add the products of the digits that are 1: 32 + 8 + 4 + 1 = 45.

Every power row on this site is laid out that way — the weights above, the digits below as cells that are inked or blank, and the products added in the line underneath. A reader who can build that row can convert a value in any base without a tool, which is the reason to learn it once, here.

What do the prefixes 0b, 0o and 0x mean?

0b marks binary, 0o octal, 0x hexadecimal; a leading # marks a hex colour. Hexadecimal is not case-sensitive (FF = ff); iToolHub writes hex digits in uppercase.

Binary (base 2)
Prefix0b
Subscript1010₂
Other forms0b1010 / 1010₂ / suffix b / % (assemblers)
Octal (base 8)
Prefix0o
Subscript17₈
Other forms0o17 / leading 0 in C (017) / 17₈ / \017 escape
Decimal (base 10)
Prefixnone (default)
Subscript17₁₀
Other formsnone (default) / 17₁₀ / suffix d (assemblers)
Hexadecimal (base 16)
Prefix0x
Subscript1F₁₆
Other forms0x1F / #1F (colours) / &H1F (BASIC) / suffix h / 1F₁₆ / \x1F escape / U+001F

A prefix exists because the digits alone are ambiguous. The value 41 is a legal decimal number, a legal hex number worth 65, and not a binary number at all, because 4 is not a binary digit. A value pasted without its prefix leaves the base to be guessed, which is why every converter here names its source base in its own title.

Older notations sit alongside the modern ones. C and several shells mark octal with a leading zero, so 017 and 0o17 carry the same value. Textbooks use a subscript instead, which is unambiguous on paper and awkward to type, and assembler dialects use a trailing letter. This site writes the prefix in prose and drops it inside a table, where the column heading already says which base the column holds.

What are a bit, a nibble, a byte and a word?

A byte is 8 bits and holds 256 values, 0–255; a nibble is 4 bits.

Widths decide two things: how a value is padded, and where it stops. A bit is one binary digit. Three bits make the group one octal digit stands for, four bits make a nibble, which is what one hex digit stands for, and eight bits make a byte. A 16-bit word is two bytes and a 32-bit word is four.

Width, distinct values and unsigned range of each data unit
UnitWidth in bitsDistinct valuesUnsigned range
bit120–1
bit triple (one octal digit)380–7
nibble (one hex digit)4160–15
byte82560–255
16-bit word16655360–65535
32-bit word3242949672960–4294967295

Padding follows the width rather than the value, so 101101 written into a byte-wide column becomes 00101101, and a character code written in octal takes three digits. iToolHub's text-to-binary and text-to-hex converters write one 8-bit byte per character, separated by spaces.

An 8-bit byte holds 0–255 unsigned; read the same eight bits as a signed value and the range becomes −128 to 127 — how to write a negative number in binary takes this further.

Where character codes fit — and where this page stops

This page converts numbers between bases. A letter is not a number until a table says which number it is, and that lookup is a different mechanism.

A letter becomes a number first; the base comes after: look up any ASCII code from 0 to 127. Above code 127 one character stops being one byte, which is why one character can become several bytes. Character codes are a separate step; to turn text into binary code, the converter shows each byte.

How do you convert decimal to any base with repeated division?

Repeated division is the method that turns a decimal value into any other base.

You divide by the target base, keep the remainder, and repeat on the quotient until nothing is left. The remainders are the digits of the answer, and they arrive backwards: the first remainder is the last digit. That is the step most people miss.

  1. Divide the decimal value by the target base.
  2. Write down the remainder; carry the quotient to the next line.
  3. Repeat until the quotient is 0.
  4. Read the remainders from bottom to top.

Take 156 into base 2. Each line divides the previous quotient by 2 and records what is left over:

Repeated division of 156 by 2, read bottom to top
Division Quotient Remainder
156 ÷ 2 78 0
78 ÷ 2 39 0
39 ÷ 2 19 1
19 ÷ 2 9 1
9 ÷ 2 4 1
4 ÷ 2 2 0
2 ÷ 2 1 0
1 ÷ 2 0 1

Reading the remainder column from the bottom line to the top gives eight digits. Result: 156 = 10011100

The same seed and the same method give the other two bases. Dividing by 8 leaves the remainders 4, 3 and 2, so 156 is 0o234 in octal. Dividing by 16 leaves 12 and 9, and a remainder of 12 is the digit C, because a remainder is a digit in the target base and 10 to 15 are written A to F. A remainder is always smaller than the base you divided by, which is exactly what makes it a digit of that base. Result: 156 = 10011100 = 0o234 = 0x9C

Applied to a single pair this becomes convert decimal to binary. The same ladder run in base 8 is how to change decimal to octal by dividing by 8, and to write a number in base 16 you divide by 16 and name each remainder.

How do you convert any base to decimal with positional expansion?

Positional expansion is the method that turns a value in any base back into decimal.

You write each digit's place value above it, multiply, and add the products. It is the inverse of repeated division, and this section runs the same seed in the opposite direction on purpose: the two sections check each other, and a reader who works both by hand has proved the pair rather than trusting it.

  1. Write the place values above the digits, right to left, starting at base⁰.
  2. Multiply each digit by its place value.
  3. Add the products.

Take 10011100, the value the ladder above produced. Every place value is twice the one to its right, so the top row of the block below is the powers of two in descending order:

valuecode
1286432168421
156
156
10011100

Result: 10011100 = 156

Only the digits that are 1 contribute, so the sum is 128 + 16 + 8 + 4, and the inked cells are the ones being added.

The other two bases work the same way with their own weights. In octal the place values are 64, 8, 1, so 0o234 expands to 2 × 64 + 3 × 8 + 4. In hex they are 16, 1, so 0x9C expands to 9 × 16 + 12, taking C as 12. Both land on 156, which is the check the pair exists for.

Applied to a single pair this becomes work out the decimal value of a binary number, with octal to decimal calculator with the working shown and hex to decimal by positional expansion for the other two starting bases.

Why can binary be grouped into octal and hex digits?

One hexadecimal digit equals exactly 4 bits (16 = 2⁴); one octal digit equals exactly 3 bits (8 = 2³). Hex and octal are therefore converted by grouping bits, and hex ↔ octal passes through binary.

The reason is the ratio. A base that is a power of two divides a bit string evenly, so each digit owns a whole number of bits and no digit straddles a boundary. Ten is not a power of two, and the bits per decimal digit come out not integral (~3.32) — the number planted in the decimal profile above. That is why decimal has to divide or multiply its way across, while octal and hex only re-cut the bits.

The two maps below are the entire shortcut: one hex digit reads off the first, one octal digit off the second.

Hex digit to 4-bit group
Hex digitDecimalBinary (4-bit)Octal
0000000
1100011
2200102
3300113
4401004
5501015
6601106
7701117
88100010
99100111
A10101012
B11101113
C12110014
D13110115
E14111016
F15111117
Octal digit to 3-bit group
Octal digitDecimalBinary (3-bit)
00000
11001
22010
33011
44100
55101
66110
77111

Grouping starts at the right-hand end, where the place values line up, and the leftmost group takes zeros when it comes up short. Take 1010111100: in threes from the right that is 001 010 111 100, and those groups name 1, 2, 7 and 4. Result: 1010111100 = 0o1274

Four bits at a time in one direction — change binary into hex four bits at a time — and four bits out in the other, convert hexadecimal to binary. Three bits at a time gives convert binary to octal, and convert octal to binary runs it in reverse.

How do you convert between octal and hex?

There is no direct digit map between octal and hex, so the bits are the bridge.

Eight and sixteen are not powers of each other, so no octal digit lines up with a hex digit. Both are powers of two, though, so both map cleanly onto bits: one octal digit is three, one hex digit is four. Lay the bits out in a single row and re-cut them at the other width.

  1. Replace each source digit with its bits — four for a hex digit, three for an octal digit.
  2. Join the bits into one string, then regroup from the right into threes (for octal) or fours (for hex); a short leftmost group takes leading zeros, which drop out of the answer.
  3. Name each group with one digit of the target base.

Forward, out of hex: 2F spreads to 0010 1111 by the hex map, and regrouping from the right into threes gives 00 101 111, which names 5 and 7. Result: 2F = 00101111 = 0o57

Back, out of octal: 472₈ spreads to 100 111 010 by the octal map, and regrouping into fours from the right gives 1 0011 1010, which names 1, 3 and A. Result: 472₈ = 100111010 = 0x13A

The slower route runs through decimal: expand the source, then divide back out. It reaches the same answer and puts back the arithmetic the shortcut avoids, so keep it as a check rather than a method.

Applied to a single pair this becomes change a hex number into octal via binary, and change an octal number into hex goes the other way. Those two pages carry the padding edge cases.

What is the number system conversion table (0-255 in all four bases)?

The table below holds every value a byte can carry, written in all four bases at once.

Read a row across: the decimal value, then the same value in eight binary digits, in three octal digits and in two hex digits, padded by the conventions set out above. Rows 0 to 31 sit open and the rest continue underneath them, and every row carries its own anchor, so one value can be linked to directly. Use it to check work done by hand, or to read a byte out of a hex dump without running the arithmetic again. Every row comes from the same conversion code as the worked examples above.

Number system conversion table, 0 to 31
DecimalBinary (8-bit)OctalHexadecimal
00000000000000
10000000100101
20000001000202
30000001100303
40000010000404
50000010100505
60000011000606
70000011100707
80000100001008
90000100101109
10000010100120A
11000010110130B
12000011000140C
13000011010150D
14000011100160E
15000011110170F
160001000002010
170001000102111
180001001002212
190001001102313
200001010002414
210001010102515
220001011002616
230001011102717
240001100003018
250001100103119
26000110100321A
27000110110331B
28000111000341C
29000111010351D
30000111100361E
31000111110371F
Show 32 to 255
Number system conversion table, 32 to 255
DecimalBinary (8-bit)OctalHexadecimal
320010000004020
330010000104121
340010001004222
350010001104323
360010010004424
370010010104525
380010011004626
390010011104727
400010100005028
410010100105129
42001010100522A
43001010110532B
44001011000542C
45001011010552D
46001011100562E
47001011110572F
480011000006030
490011000106131
500011001006232
510011001106333
520011010006434
530011010106535
540011011006636
550011011106737
560011100007038
570011100107139
58001110100723A
59001110110733B
60001111000743C
61001111010753D
62001111100763E
63001111110773F
640100000010040
650100000110141
660100001010242
670100001110343
680100010010444
690100010110545
700100011010646
710100011110747
720100100011048
730100100111149
74010010101124A
75010010111134B
76010011001144C
77010011011154D
78010011101164E
79010011111174F
800101000012050
810101000112151
820101001012252
830101001112353
840101010012454
850101010112555
860101011012656
870101011112757
880101100013058
890101100113159
90010110101325A
91010110111335B
92010111001345C
93010111011355D
94010111101365E
95010111111375F
960110000014060
970110000114161
980110001014262
990110001114363
1000110010014464
1010110010114565
1020110011014666
1030110011114767
1040110100015068
1050110100115169
106011010101526A
107011010111536B
108011011001546C
109011011011556D
110011011101566E
111011011111576F
1120111000016070
1130111000116171
1140111001016272
1150111001116373
1160111010016474
1170111010116575
1180111011016676
1190111011116777
1200111100017078
1210111100117179
122011110101727A
123011110111737B
124011111001747C
125011111011757D
126011111101767E
127011111111777F
1281000000020080
1291000000120181
1301000001020282
1311000001120383
1321000010020484
1331000010120585
1341000011020686
1351000011120787
1361000100021088
1371000100121189
138100010102128A
139100010112138B
140100011002148C
141100011012158D
142100011102168E
143100011112178F
1441001000022090
1451001000122191
1461001001022292
1471001001122393
1481001010022494
1491001010122595
1501001011022696
1511001011122797
1521001100023098
1531001100123199
154100110102329A
155100110112339B
156100111002349C
157100111012359D
158100111102369E
159100111112379F
16010100000240A0
16110100001241A1
16210100010242A2
16310100011243A3
16410100100244A4
16510100101245A5
16610100110246A6
16710100111247A7
16810101000250A8
16910101001251A9
17010101010252AA
17110101011253AB
17210101100254AC
17310101101255AD
17410101110256AE
17510101111257AF
17610110000260B0
17710110001261B1
17810110010262B2
17910110011263B3
18010110100264B4
18110110101265B5
18210110110266B6
18310110111267B7
18410111000270B8
18510111001271B9
18610111010272BA
18710111011273BB
18810111100274BC
18910111101275BD
19010111110276BE
19110111111277BF
19211000000300C0
19311000001301C1
19411000010302C2
19511000011303C3
19611000100304C4
19711000101305C5
19811000110306C6
19911000111307C7
20011001000310C8
20111001001311C9
20211001010312CA
20311001011313CB
20411001100314CC
20511001101315CD
20611001110316CE
20711001111317CF
20811010000320D0
20911010001321D1
21011010010322D2
21111010011323D3
21211010100324D4
21311010101325D5
21411010110326D6
21511010111327D7
21611011000330D8
21711011001331D9
21811011010332DA
21911011011333DB
22011011100334DC
22111011101335DD
22211011110336DE
22311011111337DF
22411100000340E0
22511100001341E1
22611100010342E2
22711100011343E3
22811100100344E4
22911100101345E5
23011100110346E6
23111100111347E7
23211101000350E8
23311101001351E9
23411101010352EA
23511101011353EB
23611101100354EC
23711101101355ED
23811101110356EE
23911101111357EF
24011110000360F0
24111110001361F1
24211110010362F2
24311110011363F3
24411110100364F4
24511110101365F5
24611110110366F6
24711110111367F7
24811111000370F8
24911111001371F9
25011111010372FA
25111111011373FB
25211111100374FC
25311111101375FD
25411111110376FE
25511111111377FF

Which number system converter do you need?

Which method you need follows from the two bases, not from the tool you happen to open.

If your value is decimal and you want any other base, you are doing repeated division. If your value is binary, octal or hex and you want decimal, you are doing positional expansion. If both bases are powers of two, you are grouping bits — and octal to hex, or hex to octal, groups them twice, because the bits have to be re-cut on the way through.

Decimal is the odd base out in all three rules, which is why it is the pivot in one direction and never a grouping partner in the other. A converter here is one of those three methods with its working shown and its edge cases handled, so the page names the pair and the method underneath is one you have already read here. Nothing after this section adds a mechanism: the three rules above are the whole map. To pick a converter rather than a method, start from every binary converter.

Frequently Asked Questions

What is decimal, and why is it the pivot for every conversion?

Decimal is base 10, the notation you already compute in, so both universal methods route through it: repeated division starts there, and positional expansion ends there. It is also the base with no bit grouping, which is why the power-of-two shortcut reads as a trick.

Where do you meet octal and hexadecimal in practice?

Octal turns up in Unix file permissions, as in chmod 755, and in escape sequences. Hex turns up in memory addresses, colour codes, MAC addresses, hex dumps and error codes. Both are compact shorthand for bit patterns, which is why they cluster around low-level work.

Every conversion on this page is one of the two universal methods, or the bit-grouping shortcut that follows when both bases are powers of two. The values change with the input; the method behind them does not.