Binary ↔ Decimal ↔ Hex Converter
Instantly convert any number between binary (base 2), decimal (base 10), hexadecimal (base 16), and octal (base 8). Type in any field and all others update live. Essential for working with registers, masks, and memory addresses.
Number Base Converter
Convert between binary (base 2), decimal (base 10), hexadecimal (base 16), and octal (base 8). Enter a number in any field and it instantly converts to all other bases. Essential for embedded programming, bitwise operations, memory addresses, and working with registers and bit masks.
Type in any field — all others update instantly
Only 0 and 1
Standard numbers 0–9
0–9, A–F (case insensitive)
Only 0–7
How number bases work
Each base uses a different number of digits per position:
- Binary (base 2): 0–1. Each position is a power of two. 1011 = 8 + 0 + 2 + 1 = 11.
- Decimal (base 10): 0–9.
- Hexadecimal (base 16): 0–9 and A–F. One hex digit represents exactly four bits (a nibble), so 0xA5 = 1010 0101.
- Octal (base 8): 0–7, three bits per digit; mostly seen in Unix file permissions.
Nibble table
| Hex | Binary | Decimal | Hex | Binary | Decimal | |
|---|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 | |
| 1 | 0001 | 1 | 9 | 1001 | 9 | |
| 2 | 0010 | 2 | A | 1010 | 10 | |
| 3 | 0011 | 3 | B | 1011 | 11 | |
| 4 | 0100 | 4 | C | 1100 | 12 | |
| 5 | 0101 | 5 | D | 1101 | 13 | |
| 6 | 0110 | 6 | E | 1110 | 14 | |
| 7 | 0111 | 7 | F | 1111 | 15 |
Memorise this table and you can convert hex to binary in your head, one digit at a time.
Where makers use hex and binary
- I2C addresses are written in hex: 0x3C for an SSD1306 OLED, 0x76/0x77 for a BME280, 0x27 for a PCF8574 LCD backpack.
- Register bits: setting bit 5 of a register is
reg |= (1 << 5), which is 0x20 or 0b00100000. - Colours: RGB565 and 24-bit colours (0xFF8800 is orange) for TFT displays and WS2812 LEDs.
- Memory addresses and flash offsets in ESP32 partition tables (0x10000).
Negative numbers
Microcontrollers store signed integers in two's complement: invert the bits and add one. In 8 bits, −1 is 0xFF and −128 is 0x80. An int16_t of 0xFFFE read from a sensor is −2, not 65 534.
Worked conversions
- Binary to decimal: 1011 0110 = 128 + 32 + 16 + 4 + 2 = 182.
- Decimal to hex: 200 ÷ 16 = 12 remainder 8, and 12 is C, so 200 = 0xC8.
- Hex to binary: 0x5A → 5 = 0101, A = 1010 → 0101 1010.
- Signed byte: 0xF6 as an unsigned byte is 246; as a signed int8_t it is 246 − 256 = −10.
Bit operations you'll use in firmware
| Task | C / Arduino code | Example (reg = 0x0F) |
|---|---|---|
| Set bit 7 | `reg | = (1 << 7)` |
| Clear bit 0 | reg &= ~(1 << 0) | 0x0E |
| Toggle bit 1 | reg ^= (1 << 1) | 0x0D |
| Test bit 3 | if (reg & (1 << 3)) | true |
| Take the high nibble | (reg >> 4) & 0x0F | 0x0 |
| Combine two bytes from a sensor | `(int16_t)((msb << 8) | lsb)` |
Reading sensor registers
Many I2C sensors return a measurement as two bytes. An MPU6050 might return 0xFC and 0x18 for an axis: combined that is 0xFC18, which as a signed 16-bit number is −1000. Treating it as unsigned would give 64 536, a common bug when values suddenly jump to huge numbers.
Formula, inputs and worked examples
Formula and method
Convert the same integer between bases 2, 8, 10, and 16; validate allowed digits for the selected base.
Worked examples
A common value
255 in decimal is 0xFF in hex, 11111111 in binary and 377 in octal.
Reading a register value
The binary value 10110010 is 128 + 32 + 16 + 2 = 178 in decimal. Group the bits in fours, 1011 0010, and read each group as a hex digit to get 0xB2.
Building a bit mask
To set bit 5, use 1 << 5 = 32 = 0x20 = 00100000. On an Arduino Uno, PORTB |= 0x20 sets PB5, which is digital pin 13.
Inputs
- Number value — Unit: integer
- Input base — Unit: 2, 8, 10, or 16
What the result means
- Converted number — Unit: binary / octal / decimal / hexadecimal
Practical tips and common mistakes
- Each hex digit is exactly four bits, so converting between hex and binary by hand is quick.
- In C and C++, a number that starts with 0 is octal: 010 equals 8, not 10.
- Signedness matters: as an 8-bit signed value 0xFF is −1, but as unsigned it is 255. Confirm the register or variable width before interpreting a value.
- Use the 0x prefix for hex and 0b for binary (supported by GCC and the Arduino toolchain).
Assumptions and limitations
- Leading zeros can matter when displaying fixed-width registers even when they do not change the numeric value.
- For embedded bit masks, confirm signedness and the intended register width.
Frequently asked questions
How do I convert binary to decimal?
Multiply each bit by its power of two and add them up, starting with 1 on the right: 1101 = 8 + 4 + 0 + 1 = 13.
How do I convert hex to binary?
Replace each hex digit with its 4-bit pattern: 0x3C = 0011 1100.
What does 0x mean?
0x is a prefix marking a hexadecimal number in C, C++, Python and most programming languages. 0b marks binary, for example 0b1010.
What is 0xFF in decimal?
255: the largest value an unsigned 8-bit byte can hold. As a signed int8 it represents −1.
Learn more
More Utilities