Computer & IT

Binary, Decimal, Hex & Octal Converter

Convert whole numbers between base 2, 8, 10 and 16 — exact for any size, with two’s complement.

Inputs

Whole numbers only, up to 512 digits. A leading minus sign is allowed. You may use the prefix for the chosen base (0b binary, 0o octal, 0x hex) and single spaces or underscores between digits, e.g. 1101 0110.

Result

Decimal (base 10)
−42
Binary (base 2)
−10 1010
Octal (base 8)
−52
Hexadecimal (base 16)
−2A
Bits needed for the magnitude
6 bits (plus a sign)
Two’s-complement bit pattern (chosen width)
1111 1111 1101 0110 (16 bits)
Same bit pattern in hexadecimal
0xFFD6
Bit pattern read as signed (two’s complement)
−42
Same bit pattern read as unsigned
65,494
Working
Step 1 – positive 42 in 16 bits: 0000 0000 0010 1010 Step 2 – invert every bit (0 ↔ 1): 1111 1111 1101 0101 Step 3 – add 1: 1111 1111 1101 0110 Result: bit pattern 1111 1111 1101 0110 = 0xFFD6 in hexadecimal (check: 2^16 − 42 = 65,494)

−42 in decimal is −10 1010 in binary, −52 in octal and −2A in hexadecimal, each written with a minus sign. In 16-bit two’s complement it is stored as the bit pattern 1111 1111 1101 0110 (0xFFD6). Read as a signed 16-bit number this pattern means −42; read as unsigned, the same bits would mean 65,494.

Educational estimate — check the assumptions below.

About this calculator

The same whole number can be written in different bases. People use base 10 (decimal); computers store numbers in base 2 (binary); programmers often write them in base 16 (hexadecimal), where each digit stands for exactly four bits, and sometimes in base 8 (octal), where each digit stands for three bits. This converter changes a whole number from any of these bases to all the others exactly — with no rounding, even for very long numbers.

The results are given in two separate groups, so they are not confused:

  • Ordinary conversions (binary, octal, hexadecimal) — the number itself written in each base, with a minus sign when it is negative. For example −128 is −1000 0000 in binary and −80 in hexadecimal.
  • Two’s-complement bit pattern — how a computer stores the number in a fixed number of bits (8, 16, 32 or 64). For example −128 in 8 bits is stored as 1000 0000, written 0x80 in hexadecimal. This pattern has no minus sign; the leftmost bit 1 marks a negative number. Read as an ordinary (unsigned) number, the same bits would mean 128, which is why the two must not be mixed up. The results show both readings of the pattern: for example 1010 1010 means −86 when read as signed and 170 when read as unsigned.

Formula

value = Σ di × bi

Two’s complement of a negative number in n bits: write the magnitude in n bits → invert every bit → add 1

(the same as 2n − magnitude)    Signed range in n bits: −2n−1 to 2n−1 − 1

di = digit in position i (counting from 0 on the right), b = base. To convert to another base, divide repeatedly by the new base and read the remainders from last to first. One hexadecimal digit = 4 bits; one octal digit = 3 bits.

Worked example

  • 214 (decimal) = 128 + 64 + 16 + 4 + 2 = 1101 0110 (binary) = 326 (octal) = D6 (hex).
  • FF (hex) = 15 × 16 + 15 = 255.
  • −1 in 8 bits: positive 1 = 0000 0001 → invert = 1111 1110 → add 1 = 1111 1111 = 0xFF.
  • −42 in 16 bits: 42 = 0000 0000 0010 1010 → invert = 1111 1111 1101 0101 → add 1 = 1111 1111 1101 0110 = 0xFFD6 (check: 216 − 42 = 65,494).
  • Signed ranges: 8 bits −128 to 127; 16 bits −32,768 to 32,767; 32 bits −2,147,483,648 to 2,147,483,647. A value outside the range is rejected, not cut off.

Assumptions and limitations

  • Whole numbers (integers) only; fractions are not converted.
  • Negative numbers are shown with a minus sign. When a number of bits is chosen, the value must be inside the signed range for that width (for 8 bits, −128 to 127); values outside it are rejected rather than truncated.
  • Up to 512 digits are accepted; all arithmetic is exact.

References

  • Positional number systems and two’s complement representation (e.g. Patterson and Hennessy, Computer Organization and Design).