Home / Tools / Binary, hex and decimal converter

Binary, hex and decimal converter

Convert numbers between decimal, hexadecimal, binary and octal, and see how signed values look in two's complement at 8, 16, 32 and 64 bits.

  • Code tool
  • Runs in your browser
  • Tested 19 November 2026
  • Sources
Prefixes like 0x and 0b are fine. Underscores and spaces are ignored.

Why programmers use hex and binary

Computers store everything in binary, base 2, where each digit is a bit. Long binary numbers are hard to read, so programmers group them into fours. Each group of four bits is exactly one hexadecimal digit, 0 to 9 then A to F. That's why the byte 1010 0101 is written 0xA5, and why microcontroller registers and colour codes are given in hex.

DecimalBinaryHexDecimalBinaryHex
000000810008
100011910019
200102101010A
300113111011B
401004121100C
501015131101D
601106141110E
701117151111F

Two's complement: how negative numbers are stored

Almost every processor stores signed integers in two's complement. In an 8-bit value, the top bit is worth −128 instead of +128, so the range is −128 to +127. To negate a number, flip every bit and add one: 5 is 0000 0101, so −5 is 1111 1011, which is 0xFB.

The same bits mean different things depending on the type. 0xC8 is 200 as an unsigned byte but −56 as a signed one. That's a classic bug when reading sensor registers: a temperature sensor that returns a signed 16-bit value has to be converted, or a reading of −1 shows up as 65,535. In Python:

raw = 0xFFF6              # 16-bit value read from a sensor
if raw & 0x8000:          # top bit set, so it's negative
    raw -= 0x10000
print(raw)                # -10

Writing numbers in code

Python, C, C++, JavaScript and Arduino all accept 0x for hex and 0b for binary, for example 0xFF and 0b1010. Python uses 0o for octal, while C and Arduino treat any number starting with 0 as octal, so 010 is 8 there. Python converts with hex(), bin() and int("ff", 16).

Sources

Where the formulas and figures on this page come from. We test every tool against worked examples from these before publishing.