Converters

Number Base Converter

Convert numbers between binary, octal, decimal, and hexadecimal.

Advertisement

Why Developers Need to Understand Number Bases

Number bases are the foundation of how computers represent and manipulate numeric data. While most high-level programming lets you work in familiar decimal notation, the underlying hardware operates on binary. Understanding how numbers map between bases is essential for debugging low-level code, interpreting protocol headers, working with cryptographic primitives, and reasoning about bitwise operations.

Different bases serve different purposes in development. Binary is the native language of the machine and shows up in bitwise logic, flag fields, and bit masks. Octal appears in Unix file permissions and certain legacy systems. Decimal is the default for human-readable output and most arithmetic. Hexadecimal provides a compact representation for binary data and dominates in memory addresses, color values, and cryptographic hashes.

A solid grasp of number bases also helps you read and write code more fluently. Most languages support literals in multiple bases — 0x for hex, 0b for binary, 0o for octal in modern syntax — and knowing when to use each makes your code more readable. A flag field expressed as 0x1F is clearer than 31, and a bit mask written as 0b1010 makes its intent more obvious than 10.

The Most Common Number Bases in Computing

Binary, or base 2, uses only the digits 0 and 1. Each position represents a power of 2, so the binary value 1010 equals 1×8 + 0×4 + 1×2 + 0×1, which is 10 in decimal. Binary is the fundamental representation used by all digital hardware, and every other base is ultimately a human convenience layered on top of it.

Octal, or base 8, uses digits 0 through 7. Each octal digit corresponds to exactly three binary digits, which made it a natural choice for early computers that used 12-bit, 24-bit, or 36-bit word sizes. Today, octal is most commonly seen in Unix file permissions, where each digit represents read, write, and execute bits for the owner, group, and others.

Decimal, or base 10, is the system humans use by default, with digits 0 through 9. It is not a natural fit for binary hardware — converting between decimal and binary requires non-trivial arithmetic — but it is the format users expect for all displayed numeric values. Hexadecimal, or base 16, uses digits 0 through 9 and letters A through F. Each hex digit represents four binary digits, making it the standard compact representation for binary data in source code, documentation, and debugging output.

  • Base 2 (binary): native hardware representation, used in bitwise logic
  • Base 8 (octal): Unix file permissions, legacy system compatibility
  • Base 10 (decimal): default for human-readable numeric output
  • Base 16 (hexadecimal): compact representation for binary data
  • Base 64: encoding scheme for safe binary transport as text

How Manual Base Conversion Works

Converting from any base to decimal uses positional notation. For each digit in the source number, multiply the digit by the base raised to the power of its position (counting from right, starting at 0), then sum the results. The hex value 0x2F equals 2×16 + 15×1, which is 47 in decimal. This method works for any source base, provided you correctly interpret each digit value.

Converting from decimal to another base uses repeated division. Divide the decimal value by the target base, record the remainder, then divide the quotient again, repeating until the quotient is zero. The digits of the result, read from last remainder to first, form the number in the target base. To convert 47 to hex: 47 ÷ 16 = 2 remainder 15, 2 ÷ 16 = 0 remainder 2, so the result is 2F.

Conversions between bases that are powers of 2 — binary, octal, and hex — bypass decimal entirely. Each octal digit expands to exactly three binary digits, and each hex digit expands to exactly four. To convert hex to binary, simply replace each hex digit with its 4-bit equivalent. To convert binary to hex, group bits into nibbles from the right and replace each with its hex digit. These conversions are mechanical and exact, with no arithmetic required.

Use Cases for Number Base Conversion

Debugging low-level code is a frequent scenario. Memory addresses, register values, and bytecode are typically displayed in hex for compactness, but understanding what they mean often requires converting to binary to inspect individual bits. A flag register showing 0x0040 makes more sense when you convert it to binary and see that bit 6 is set, identifying the specific flag that is active.

Cryptography and hashing produce output that is almost always expressed in hex. SHA-256 produces a 64-character hex string representing 32 bytes of binary data. When verifying a hash, comparing hashes, or interpreting intermediate values during algorithm debugging, you routinely convert between hex and binary to confirm that each step is producing the expected output.

Configuration and documentation benefit from base conversion as well. Color values in CSS can be written in hex (such as #FF5733) or in decimal RGB triplets (255, 87, 51). Network masks, permission bits, and feature flags are often easier to understand when expressed in binary. A good converter lets you switch representations to match the context in which the value will be used or read.

  • Debugging memory addresses and register values
  • Inspecting cryptographic hashes and key material
  • Working with bitwise flags and permission masks
  • Converting color values between hex and RGB representations
  • Interpreting network protocol headers and packet captures

Common Pitfalls in Base Conversion

One frequent mistake is confusing string representation with numeric value. The string "10" means ten in decimal, two in binary, eight in octal, and sixteen in hex. When passing values between systems, always be explicit about which base is intended. A common bug is treating a hex string as a decimal string or vice versa, producing values that are wildly wrong but appear plausible at first glance.

Leading zeros cause confusion, especially in binary. The value 0001 is numerically equal to 1, but in a fixed-width field, the leading zeros carry meaning — they indicate that the value occupies a specific number of bits. When converting, be clear whether you want the minimal representation or a fixed-width result, and pad or trim accordingly.

Another pitfall is signed versus unsigned interpretation. The byte 0xFF can represent either 255 (unsigned) or -1 (signed, in two's complement) depending on context. Converting between bases does not change the underlying bits, but the meaning of those bits depends on how they are interpreted. Always be aware of the signedness conventions in play when working with multi-byte values.

Best Practices for Working With Different Number Bases

Use language-native parsing and formatting functions rather than hand-rolled conversion code. JavaScript provides parseInt(value, radix) and Number(value).toString(radix). Python offers int(value, base) and the bin, oct, and hex functions. Go includes strconv.ParseInt and strconv.FormatInt. These functions handle edge cases correctly, including case-insensitive hex parsing, leading zero handling, and validation of invalid digits.

Be explicit about base when writing numeric literals in code. Modern languages support prefixes like 0x for hex, 0b for binary, and 0o for octal, which makes the intent clear to readers. Avoid bare decimal literals when the value has a specific meaning in another base — a flag mask written as 0x00FF is clearer than 255, and a permission value written as 0o755 is clearer than 493.

Finally, document the base and meaning of any non-decimal values in your code. A comment explaining that 0xFF is the maximum byte value, or that 0b1010 enables features 1 and 3, saves future readers from having to perform the conversion themselves. For values that appear in configuration files or external interfaces, document the expected base in the schema or specification so that consumers interpret the values correctly.