Number base converter

Convert whole numbers between binary, octal, decimal and hex, or any base from 2 to 36. Type in any box and the others update. Huge numbers stay exact, and you can see the two’s complement bit pattern for negative values.

Two’s complement width for negative numbers

Type in any box and the rest update. Spaces, underscores and commas between digits are fine, and so are prefixes like 0x.

In decimal

255

How number bases work

A base is how many different digits you count with before you carry. Decimal (base 10) uses 0 to 9. Binary (base 2) uses 0 and 1. Octal (base 8) uses 0 to 7. Hexadecimal (base 16) uses 0 to 9 and then A to F for 10 to 15. Bases up to 36 carry on through the alphabet to Z.

Each place is worth the base times the place to its right. In decimal, 255 is 2 × 100 + 5 × 10 + 5 × 1. In hex, FF is 15 × 16 + 15 × 1, which is also 255. In binary, 11111111 is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1, again 255.

Why programmers use hex and binary

Computers work in bits, and long strings of 1s and 0s are hard to read. Each hex digit stands for exactly 4 bits, so a byte is always two hex digits and you can convert in your head a digit at a time: 1010 1111 is AF. Octal does the same with groups of 3 bits, which is why Unix file permissions are written in octal (see the chmod calculator).

DecimalBinaryOctalHex
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F

Prefixes and separators

Code marks the base with a prefix: 0x for hex, 0b for binary and 0o for octal (some older languages use a leading 0 for octal, which is a common source of bugs). You can paste values with or without the prefix. Spaces, underscores and commas between digits are ignored, so 1111_0000, 1,048,576 and DE AD BE EF all work. In the Decimal box, a prefix switches the base for you, and assembly-style 0FFh and CSS-style #ff are read as hex.

Two’s complement and negative numbers

Computers store negative whole numbers using two’s complement. In a fixed number of bits, the top bit counts as a large negative value instead of a positive one. In 8 bits, that top bit is worth −128 instead of +128.

A worked example: −5 in 8 bits. Write 5 in binary, 0000 0101. Flip every bit to get 1111 1010, then add 1: 1111 1011, or 0xFB. Read the same bits as unsigned and you get 251. That’s why the same hex value can mean two different numbers.

Pick a width above to see the bit pattern, both readings and the bytes in big-endian and little-endian order. Auto picks the smallest of 8, 16, 32, 64 or 128 bits that holds the number. Here’s what each width holds:

BitsUnsigned rangeSigned range
80 to 255-128 to 127
160 to 65,535-32,768 to 32,767
320 to 4,294,967,295-2,147,483,648 to 2,147,483,647
640 to 18,446,744,073,709,551,615-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Big-endian and little-endian

A number bigger than one byte has to be stored as several bytes, and there are two orders to put them in. Big-endian puts the most significant byte first, the way you’d write it down; it’s used in network protocols. Little-endian puts the least significant byte first; it’s what x86 and most ARM processors use in memory. So the 32-bit value 0x12345678 sits in memory as 78 56 34 12 on a typical laptop.

Common mistakes

  • Losing precision. JavaScript numbers, spreadsheets and many online tools round anything above 253. Hashes, IDs and 64-bit values need exact maths, which this converter uses.
  • Reading signed as unsigned. If a value from a file or a network packet looks huge, it may be a small negative number. Pick the width and look at the signed reading.
  • Leading-zero octal. In C and older JavaScript, 010 is 8, not 10.
  • Mixing up 0 and O, 1 and l. In bases above 10, letters are digits. The converter reads letters in either case.

Need to encode bytes as text instead? Try Base64 encode and decode. Working out IP ranges in binary? The subnet calculator shows every address and mask in binary too.

Questions people ask

How do I convert binary to decimal?
Give each digit a place value, doubling from the right: 1, 2, 4, 8, 16 and so on. Add up the place values where there’s a 1. For 1011 that’s 8 + 2 + 1 = 11. Type it in the Binary box above to check.
How do I convert decimal to hex?
Divide by 16 and note the remainder, then divide the result by 16 again, until you reach 0. The remainders, read from last to first, are the hex digits (10 to 15 are A to F). 255 ÷ 16 is 15 remainder 15, then 15 ÷ 16 is 0 remainder 15, so 255 is FF.
What is two’s complement?
The way almost every computer stores negative whole numbers. To make a number negative, flip every bit and add 1. In 8 bits, 5 is 00000101, so −5 is 11111010 + 1 = 11111011. The top bit tells you the sign: 1 means negative when the value is read as signed.
Why does 0xFF show as both 255 and −1?
It’s the same 8 bits read two ways. As an unsigned number, 11111111 is 255. As a signed 8-bit two’s complement number, the top bit is the sign, so it’s −1. Which one is right depends on the type in your code, such as uint8 or int8.
Can it handle very large numbers?
Yes. It uses exact whole-number maths (JavaScript’s BigInt), so a 100-digit number converts without rounding. Ordinary JavaScript numbers lose accuracy above 9,007,199,254,740,991, which is why some converters give wrong answers for big hex values.
Can I convert fractions or decimals?
Not here: this converter is for whole numbers. Fractions like 0.1 often can’t be written exactly in binary, which is why 0.1 + 0.2 isn’t exactly 0.3 in most programming languages.
↑↓ move↵ openesc close