Convert between binary, octal, decimal, and hexadecimal

A conversion table for binary, octal, decimal and hexadecimal, with worked examples, Python snippets, and the copy/paste and readability checks that matter before you share converted numbers.

Converting a number between binary, octal, decimal and hexadecimal changes how the value is written, not the value itself. That makes base conversion one of the safest text transformations available, as long as the reader knows which base they are looking at. Most practical problems come from lost context: a missing prefix, stripped leading zeros, or confusion between converting a number and encoding text into bytes. This review covers what the conversion does, a reference table, how to do it by hand or in Python, and when the result stays usable after copying or sharing.

Is base conversion suitable for your intended use?

Base conversion fits work where the number's structure matters. Binary shows individual bits, which helps with flags, masks and permissions. Octal groups bits in threes and is still used for Unix file permissions such as 755. Hexadecimal groups bits in fours, so one byte is always two hex digits. That compactness is why hex appears in memory addresses, color codes, checksums and byte dumps. Decimal remains the clearest form for quantities that people read, compare or do arithmetic on.

The main benefit is that the value survives exactly. The deciding tradeoff is readability. Non-decimal forms are only meaningful to readers who know the base, and long binary strings are hard to scan or check by eye. Use decimal when the audience is general, when the number is a count or amount, or when it will be entered into forms, spreadsheets or documents that expect ordinary numbers. Use another base when the reader needs to see bit or byte structure, and label it clearly.

What the conversion actually changes

A positional number system writes a value as digits multiplied by powers of a base. Binary uses base 2 with digits 0–1, octal uses base 8 with digits 0–7, decimal uses base 10, and hexadecimal uses base 16 with digits 0–9 followed by A–F for ten through fifteen. Converting between them rewrites the digit sequence and keeps the quantity the same. Nothing is reversed, mirrored or restyled, and no visual effect is involved. The stored characters change, but the value they represent does not.

There is one ambiguity worth settling first. A "text to binary" or "text to hex" tool usually does something different from number conversion. It encodes each character into bytes using a character encoding, then shows those bytes in binary or hex. The letter A becomes 01000001 in binary or 41 in hex under ASCII and UTF-8. That result depends on the encoding. The character é is the single byte E9 in Latin-1 but the two bytes C3 A9 in UTF-8. If your input is a number, you want base conversion. If your input is words, you want character encoding, and you should confirm which encoding the tool uses.

Conversion table: decimal, binary, octal and hexadecimal

The values 0 through 15 cover every single hex digit and every four-bit binary pattern. Binary is shown padded to four bits so the columns line up.

Decimal | Binary | Octal | Hexadecimal
0 | 0000 | 0 | 0
1 | 0001 | 1 | 1
2 | 0010 | 2 | 2
3 | 0011 | 3 | 3
4 | 0100 | 4 | 4
5 | 0101 | 5 | 5
6 | 0110 | 6 | 6
7 | 0111 | 7 | 7
8 | 1000 | 10 | 8
9 | 1001 | 11 | 9
10 | 1010 | 12 | A
11 | 1011 | 13 | B
12 | 1100 | 14 | C
13 | 1101 | 15 | D
14 | 1110 | 16 | E
15 | 1111 | 17 | F

These larger reference points come up often in computing:

Decimal | Binary | Octal | Hexadecimal
16 | 10000 | 20 | 10
32 | 100000 | 40 | 20
64 | 1000000 | 100 | 40
100 | 1100100 | 144 | 64
127 | 1111111 | 177 | 7F
128 | 10000000 | 200 | 80
255 | 11111111 | 377 | FF
256 | 100000000 | 400 | 100

Converting binary to octal, hexadecimal and decimal by hand

Binary converts to octal and hex without arithmetic, because 8 and 16 are powers of 2. Group the bits from the right, in threes for octal or fours for hex, and replace each group using the table above. Decimal requires adding up the place values.

  • Example input: binary 11010110
  • To octal: group in threes from the right as 11 | 010 | 110, which gives 3 | 2 | 6, so the result is 326.
  • To hexadecimal: group in fours as 1101 | 0110, which gives D | 6, so the result is D6.
  • To decimal: add the place values of the 1 bits, 128 + 64 + 16 + 4 + 2 = 214.
  • Check: octal 326 is 3×64 + 2×8 + 6 = 214, and hex D6 is 13×16 + 6 = 214.

To go from decimal to another base, divide repeatedly by the target base and read the remainders from last to first. For hex, remainders of 10–15 become A–F. Going from octal to hex is easiest by way of binary. Expand each octal digit to three bits, then regroup into fours.

Converting decimal, octal, hexadecimal and binary in Python

Python's built-in functions handle integer conversion in both directions. Running them locally also keeps the values on your own machine, which matters if the numbers are sensitive.

n = 214

bin(n)            # '0b11010110'
oct(n)            # '0o326'
hex(n)            # '0xd6'

format(n, 'b')    # '11010110'   (no prefix)
format(n, '08b')  # '11010110'   (padded to 8 bits)
format(n, 'o')    # '326'
format(n, 'X')    # 'D6'         (uppercase hex)
f"{n:#x}"         # '0xd6'

int('11010110', 2)    # 214
int('326', 8)         # 214
int('D6', 16)         # 214
int('0b11010110', 0)  # 214  (base 0 reads the prefix)

Two behaviors are easy to miss. First, negative numbers come out with a minus sign, so bin(-5) returns '-0b101' rather than a fixed-width two's-complement pattern. To get the bit pattern a machine stores, mask the value to a width, as in format(-5 & 0xFF, '08b'), which returns '11111011'. Second, these functions work only on integers. Fractional values need different handling, and many decimal fractions, such as 0.1, have no finite binary representation.

Will the result stay readable and usable after copying or sharing?

The digits themselves are ordinary ASCII characters. Copying them does not involve special Unicode symbols, styled glyphs or right-to-left display issues. The risks come from losing context about the base or the width.

  • Ambiguous base: "10" means two, eight, ten or sixteen depending on the base. Keep a prefix (0b, 0o, 0x) or a label whenever the base is not obvious from the setting.
  • Prefix conventions differ: Python uses 0b, 0o and 0x. Some assembly notations use a trailing h. CSS colors use a leading # with hex digits. A destination may not recognize a prefix from another convention.
  • Leading zeros: padding does not change the value, but it carries meaning in fixed-width fields such as bytes and bit masks. Some environments treat a leading zero as an octal marker. In C, 010 means eight. In Python 3, 010 is a syntax error.
  • Destinations that reinterpret numbers: any field that auto-detects numbers may strip leading zeros, drop a prefix or reformat long digit strings. Check the specific destination before relying on it, and store values as text when exact digits matter.
  • Case: hex A–F and a–f represent the same values. Some systems compare strings literally, though, so match the expected case for identifiers and checksums.

Accessibility deserves a deliberate choice. A long run of 0s and 1s, or a long hex string, is hard for anyone to read accurately. How assistive technology announces such strings can vary with the software and its settings. It may read them digit by digit or as one large number, and this has not been tested for this article. Grouping digits helps both sighted readers and listeners: 1101 0110 for binary, or byte pairs for hex. So does stating the base in surrounding text. When the purpose is to communicate a quantity rather than a bit pattern, also give the decimal equivalent.

Searching and editing are also affected. A document that writes a value as 0xD6 will not match a search for 214. Including both forms where readers might search for either avoids dead ends.

Choosing a hex, binary and decimal converter tool

No specific converter software or online tool was evaluated for this article, so none is recommended by name. When judging one, check these points:

  • Does it handle numbers larger than 32 or 64 bits without silently losing precision?
  • Does it state how negative numbers are shown: a minus sign, or two's complement at a chosen width?
  • Can you control padding, prefixes and uppercase or lowercase hex?
  • Does it say clearly whether it converts numbers or encodes text into bytes, and which character encoding it uses for text?
  • Where are the values processed? Look for a documented statement rather than inferring privacy from the tool's design or marketing. For sensitive values, a local method such as the Python snippets above avoids the question entirely.

Limits of this review

The conversions, table values and Python outputs above follow from the definitions of the number systems and from documented language behavior, and the worked example has been cross-checked arithmetically. This article does not include tests of particular converter tools, spreadsheet or messaging destinations, or screen readers. Statements about those are framed as risks to check, not observed results.