Number Base Converter

Convert a number between binary, octal, decimal and hex at once: type in any box and the others follow. The Custom base box covers any base from 2 to 36, and the Two's complement panel shows negative numbers at 8, 16, 32 and 64 bits.

base 10
base 16
base 8
base 2
Two's complement

1 Number Base Converter example

One byte, four ways

1
2
3
4
Decimal      255
Hexadecimal  FF
Octal        377
Binary       1111 1111

2 Each base has its own job

Hexadecimal

One hex digit is exactly four bits, so a byte is always two digits. That is why colors are written #RRGGBB, memory addresses are hex, and a SHA-256 hash is 64 characters.

Binary

Use binary for permission bits, bitmask flags and anything where individual bits matter. The Binary box groups digits in eights, one byte per group.

Octal

You mostly meet octal in Unix file permissions, where 755 is three groups of three bits: rwxr-xr-x. That history is also why a leading zero means octal in C, so 010 equals 8.

Base 36

Base 36 uses the digits plus a to z. It shows up in short URLs and compact IDs, where you want the shortest string that is still safe to type.

3 Big numbers stay exact

Conversion uses BigInt, so large values stay exact. JavaScript numbers lose precision above 253, and parseInt gives no error when that happens. For example, ffffffffffffffff in hex comes back from parseInt as 18446744073709552000, which is off by 385. Paste a full SHA-256 in hex and the decimal you get here is exact.

Two's complement is shown separately because it needs a fixed width. The same negative number has a different bit pattern at each width: -5 is FB as an 8-bit value and FFFFFFFB as a 32-bit one. The tool lists all four widths instead of guessing which one you meant, and marks a value that does not fit as out of range.

How to use the Number Base Converter tool

  1. Type a number into the Decimal, Hexadecimal, Octal or Binary box. The other boxes update as you type.
  2. Set Custom base to anything from 2 to 36 for a fifth representation.
  3. Read the Two's complement panel for the hex bit pattern of a negative number at 8, 16, 32 and 64 bits.
  4. Click Copy All to put every base on the clipboard at once.

How to convert between bases in code

JavaScript

parseInt("ff", 16);        // 255
parseInt("11111111", 2);   // 255
(255).toString(16);        // "ff"
(255).toString(2);         // "11111111"

// Above 2^53 this silently goes wrong:
parseInt("ffffffffffffffff", 16);  // 18446744073709552000  <- not the answer

// BigInt is exact
BigInt("0xffffffffffffffff");      // 18446744073709551615n
BigInt("0b11111111");              // 255n
(255n).toString(16);               // "ff"

// BigInt has no radix parser, so build one for arbitrary bases
const parseBig = (s, base) =>
    [...s.toLowerCase()].reduce(
        (acc, c) => acc * BigInt(base) + BigInt("0123456789abcdefghijklmnopqrstuvwxyz".indexOf(c)),
        0n,
    );

The parseInt line above is why this tool uses BigInt. It returns a number that looks plausible and is wrong by 385.

Python

int("ff", 16)        # 255
int("11111111", 2)   # 255
int("0o377", 0)      # 255, base 0 reads the prefix

hex(255)             # '0xff'
bin(255)             # '0b11111111'
oct(255)             # '0o377'
format(255, "08b")   # '11111111', padded, no prefix

# Python integers are arbitrary precision, so this is simply correct
int("ffffffffffffffff", 16)   # 18446744073709551615

# Two's complement at a fixed width
(-5 & 0xFF)          # 251  -> 0xFB as a byte

PHP

<?php
hexdec('ff');            // 255
bindec('11111111');      // 255
dechex(255);             // 'ff'
decbin(255);             // '11111111'

// Any base from 2 to 36
base_convert('ff', 16, 2);   // '11111111'

// base_convert loses precision above PHP_INT_MAX.
// For big values use GMP or BCMath:
gmp_strval(gmp_init('ffffffffffffffff', 16), 10);
// '18446744073709551615'

Common errors and how to fix them

A large hex value converts to the wrong decimal
The value went through a float. parseInt goes wrong above 2^53 and base_convert above PHP_INT_MAX. Use BigInt, Python ints or GMP.
parseInt("08") returns 8 but old code returned 0
A leading zero used to mean octal. ES5 removed that behavior, but we still recommend passing the radix explicitly: parseInt(s, 10).
A negative number shows as FFFFFFFB rather than -5
That is -5 in two's complement at 32 bits, and it is correct. The pattern depends on the width, which is why the tool lists all four.
Bitwise operations give strange results on large numbers
JavaScript's bitwise operators convert their operands to 32-bit signed integers. For wider values, use the BigInt operators, which have no width limit.

Good to know

One hex digit is exactly four bits, which is why hex shows up wherever binary does: two digits per byte, six per color, 64 characters for a SHA-256 hash. Most developers meet octal in one place, Unix file permissions, where 755 is three groups of three bits spelling rwxr-xr-x. The same history is why a leading zero means octal in C, so 010 is 8.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Which bases can it convert?

Binary, octal, decimal and hexadecimal side by side, plus any base from 2 to 36 in the Custom base field. Above 10 the digits continue into the alphabet, so base 16 uses a to f and base 36 runs to z.

Can it handle very large numbers?

Yes. Conversion uses BigInt, so a 256-bit hash or a 64-bit ID converts exactly. parseInt loses precision past 2^53 without any error: ffffffffffffffff comes back 385 too high, and that is how wrong IDs end up in a database.

What about negative numbers?

A minus sign is carried through to every base. Two's complement needs a fixed width, so the tool shows the 8, 16, 32 and 64-bit patterns separately. For example, -5 is FB at 8 bits and FFFFFFFB at 32 bits.

Are decimals supported?

No, whole numbers only. Many fractions have no exact representation in other bases, so a fractional converter has to round, and the tool rejects the input instead of rounding it without telling you.

Why is 0.1 + 0.2 not 0.3?

Because 0.1 has no exact representation in binary, just as one third has none in decimal. In binary it is 0.000110011... with the 0011 repeating forever, so a 64-bit float stores the nearest value it can. This tool converts whole numbers only, so it cannot show that expansion.

Related

To see the bytes behind text instead of a number, use text to binary. Hex colors are handled by the color converter, and epoch values by the Unix timestamp converter.

More Data Converters

Browse all data converters

Copied to clipboard!