Number Base Converter
Any size · 2's complement · Steps
Try:
Type a number in any box.
Bits

Show the working

How to use this number base converter

  1. Type or paste a number into any box: decimal, hex, binary, octal, or the last box with any base from 2 to 36. All other boxes update as you type. Spaces, underscores and the prefixes 0x, 0b and 0o are allowed, and #FF8800 works in the hex box.
  2. Negative numbers: type a minus sign, such as -42. To see how a computer stores it, choose a Bit width (8, 16, 32, 64 or 128): binary, octal and hex then show the two's complement bits. With Signed ticked, a hex or binary value with the top bit set (for example FF in 8 bits) is read as a negative number; untick it to read the bits as unsigned.
  3. Fractions: numbers such as 0.625 or 1A.8 work too. When the digits never end, the box shows the first 32 and the message below the boxes shows the repeating part in brackets.
  4. Bits: click any bit to flip it. Below the bits you see how many bits the number needs, its bytes in both byte orders, and the integer types it fits in.
  5. Show the working explains the conversion step by step, the same way you would do it on paper. It picks the method that fits the box you typed in; choose another one from the list.

There is no size limit for whole numbers: a 300-digit number converts exactly. Nothing you type leaves your browser.

How number bases work

A number base says how many different digits there are and how much each place is worth. In decimal (base 10) the places are worth 1, 10, 100, 1000. In binary (base 2) there are only the digits 0 and 1, and the places are worth 1, 2, 4, 8, 16. In hexadecimal (base 16) the digits are 0–9 and A–F (A = 10 … F = 15), and the places are worth 1, 16, 256, 4096. Octal (base 8) uses 0–7.

Computers work in binary. Programmers write the same bits in hex because one hex digit is exactly 4 bits, so a byte is always two hex digits: 1111 1111 = FF. Octal is used mainly for Unix file permissions such as chmod 755.

DecimalBinaryOctalHex
0000
1111
21022
31133
410044
510155
611066
711177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F
161 00002010
3210 00004020
64100 000010040
100110 010014464
127111 11111777F
1281000 000020080
2551111 1111377FF
2561 0000 0000400100
1,00011 1110 100017503E8
1,024100 0000 00002000400
4,0961 0000 0000 0000100001000
65,5351111 1111 1111 1111177777FFFF

How to convert between binary, decimal and hex by hand

Decimal to binary: divide by 2

Divide the number by 2 and write down the remainder. Divide the quotient by 2 again, and repeat until the quotient is 0. The remainders, read from the last to the first, are the binary number. For 156 the remainders are 0, 0, 1, 1, 1, 0, 0, 1, so 156 = 1001 1100. Decimal to hex works the same way with 16: 156 ÷ 16 = 9 remainder 12 (C), so 156 = 9C.

Binary to decimal: add the place values

Write the place values 1, 2, 4, 8, 16 … under the digits from the right, and add the places that have a 1. 1011 0110 = 128 + 32 + 16 + 4 + 2 = 182. For hex, multiply each digit by 1, 16, 256, 4096 …: 2F = 2 × 16 + 15 = 47.

Binary to hex and octal: group the bits

Because 16 = 24, every group of 4 bits (from the right) is one hex digit: 1001 1100 → 9 C. Because 8 = 23, every group of 3 bits is one octal digit: 10 011 100 → 2 3 4, so 156 = 234 in octal. Going back, replace every hex digit with its 4 bits.

Fractions

For the part after the point, multiply by the new base instead of dividing: 0.625 × 2 = 1.25, 0.25 × 2 = 0.5, 0.5 × 2 = 1.0, so 0.625 = 0.101 in binary. The whole-number parts of the results are the digits, read from the first to the last. Some fractions never end: 0.1 in binary is 0.0(0011), where the digits in brackets repeat forever. That is why 0.1 + 0.2 is 0.30000000000000004 in most programming languages.

Negative numbers and two's complement

Computers store a negative whole number in two's complement: to write -x in N bits, write x in binary, flip every bit and add 1. The top bit then works as the sign (1 = negative). The same bits can be read in two ways, which is why FF is 255 as an unsigned byte and -1 as a signed byte.

Example in 8 bitsBinaryHex
50000 010105
Flip every bit1111 1010FA
Add 1 → -51111 1011FB
-11111 1111FF
-128 (smallest)1000 000080
TypeBitsSigned rangeUnsigned range
byte / int88-128 to 1270 to 255
short / int1616-32,768 to 32,7670 to 65,535
int / int3232-2,147,483,648 to 2,147,483,6470 to 4,294,967,295
long / int6464-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070 to 18,446,744,073,709,551,615

Base conversion in code

LanguageText to numberNumber to text
JavaScriptparseInt('ff', 16), BigInt('0x' + s)n.toString(2), n.toString(16), 32-bit: (n >>> 0).toString(2)
Pythonint('ff', 16), int('0b1010', 0)bin(n), hex(n), oct(n), format(n, '08b'), 8-bit: format(n & 0xFF, '08b')
JavaInteger.parseInt("ff", 16), new BigInteger(s, 36)Integer.toBinaryString(n), Integer.toHexString(n), Integer.toString(n, 36)
C#Convert.ToInt32("ff", 16)Convert.ToString(n, 2), n.ToString("X")
C / C++strtol(s, NULL, 16)printf("%x %o", n, n), std::bitset<8>(n).to_string()
Gostrconv.ParseInt(s, 16, 64)strconv.FormatInt(n, 2), fmt.Sprintf("%b %x %o", n, n, n)
Bashecho $((16#ff)), echo $((2#1010))printf '%x %o\n' 255 255, echo 'obase=2; 255' | bc

Java toHexString and toBinaryString print negative numbers as their 32-bit two's complement, while Python bin(-5) prints -0b101. Mask with & 0xFF (or the width you need) to get the bits.

Frequently asked questions

How do I convert binary to decimal?

Multiply each binary digit by its place value and add the results. The place values are powers of 2 counted from the right: 1, 2, 4, 8, 16, 32, 64, 128 and so on. For 1011 0110 that is 128 + 32 + 16 + 4 + 2 = 182. Type a binary number above to see every step.

How do I convert decimal to binary?

Divide the number by 2 again and again and write down each remainder (0 or 1) until the quotient is 0. Read the remainders from the last one to the first one. 156 gives the remainders 0, 0, 1, 1, 1, 0, 0, 1, so 156 is 1001 1100 in binary.

How do I convert hex to decimal?

Each hex digit is worth 16 times the digit on its right, and the letters A to F stand for 10 to 15. For 2F: 2 × 16 + 15 = 47. For FF: 15 × 16 + 15 = 255. Hex is popular because one hex digit is exactly 4 bits, so two hex digits are one byte.

What is 255 in binary and hex?

255 is 1111 1111 in binary, FF in hex and 377 in octal. It is the largest number that fits in one byte (8 bits), which is why it appears in IP addresses, RGB colours such as #FFFFFF and many file formats.

What is two's complement?

Two's complement is how computers store negative whole numbers. To write -x in N bits, write x in binary, flip every bit and add 1. In 8 bits, -5 is 1111 1011 (FB in hex) and -1 is 1111 1111. Choose a bit width above to see the two's complement form, and tick Signed to read a hex or binary value as a negative number.

Why is 0.1 not exact in binary?

A binary fraction can only be a sum of 1/2, 1/4, 1/8 and so on. 0.1 is not such a sum, so in binary its digits repeat forever: 0.0001100110011… or 0.0(0011). Computers cut it off after 52 bits, which is why 0.1 + 0.2 gives 0.30000000000000004 in JavaScript, Python and most other languages.

What do 0x, 0b and 0o mean?

They are prefixes that say which base a number is written in: 0x for hexadecimal (0xFF), 0b for binary (0b1010) and 0o for octal (0o755). Each box here accepts its own prefix, and the decimal box also recognises 0x, 0b and 0o and converts from that base. Spaces and underscores, as in 1111_0000, are ignored.

More developer tools