CalcHub

Number Base Converter

Sooner or later you need to know which three numbers are hiding in #1E90FF, what the 7 and the 5 in chmod 755 actually allow, or why a debugger shows F6 for a variable you know holds -10. All of those questions come down to moving between number bases. Pick binary, octal, decimal, or hexadecimal as the input base, type an integer, and this converter shows the other bases at the same time, with the binary result split into groups of four so each group lines up with one hex digit. The math runs on JavaScript BigInt, so integers dozens of digits long convert without rounding. A few things are worth knowing up front: a 0x, 0o, or 0b prefix that matches the selected base is skipped, as are spaces and underscores between digits; fractions are not accepted; and negative numbers are shown with a minus sign rather than in two's complement. The examples and tables below show how to get two's complement out of it anyway.

Key points

  • A prefix that matches the selected tab is skipped: 0xFF in the hex tab reads as FF, 0b1010 in the binary tab as 1010, and 0o755 in the octal tab as 755. Hex letters a–f and prefixes work in either case, and results are always shown in uppercase.
  • Enter a negative number with a leading - or the ± button next to the input, and the result keeps its sign: -10 becomes -1010. For 8-bit two's complement, enter 256 − 10 = 246 in decimal and you get 1111 0110 (F6).
  • The spaces in the binary result are only there for readability, so you can copy the result and paste it straight back into the binary tab. Spaces are ignored, and so are underscores as in 1111_0110. Switching tabs clears the input.

Integers only (no decimals; for negatives type a leading - or press ±)

Examples

Turning the color #1E90FF into RGB values

A web color written as #RRGGBB is three two-digit hex numbers, one each for the red, green, and blue channels, each running from 0 to 255. In the hexadecimal tab, 1E gives 30, 90 gives 144, and FF gives 255, so #1E90FF is rgb(30, 144, 255), the shade usually called dodger blue. Enter all six digits at once and you get 2,003,199, which is the three channels packed into a single 24-bit integer (30×65,536 + 144×256 + 255), so for color work you need to convert two digits at a time. Going the other way, if a designer hands you rgb(255, 128, 0), enter 255, 128, and 0 in the decimal tab to get FF, 80, and 0, pad the single 0 to 00, and write #FF8000.

Reading chmod 755 and 644 as bits

A Linux permission mode is one octal digit each for the owner, the group, and everyone else, where each digit is the sum of read (4), write (2), and execute (1). Enter 755 in the octal tab and you get binary 1 1110 1101, or 493 in decimal. This tool groups binary in fours, but permissions come in threes, so regroup from the right as 111 101 101. Every 1 is a granted permission, giving rwx r-x r-x: the owner can do everything, while the group and others can read and execute. 644 is 110 100 100 (rw- r-- r--), or 420 in decimal. That decimal value is exactly what you write when a config format such as JSON has no octal notation and expects the mode as a plain number.

Writing -10 in 8-, 16-, and 32-bit two's complement

Enter -10 in the decimal tab and you get binary -1010 and hex -A. That is mathematically correct, but it is not what sits in memory or a register. In a fixed width of n bits, a negative value -x is stored as 2ⁿ − x, so for 8 bits you enter 256 − 10 = 246 in decimal and get 1111 0110, or F6. For 16 bits enter 65,536 − 10 = 65,526 (FFF6), and for 32 bits enter 4,294,967,296 − 10 = 4,294,967,286 (FFFFFFF6). Adding bits only fills the front with 1s, or Fs in hex, while the low digits stay the same. To go backwards, if a dump shows F6 and you know it is a signed 8-bit value, enter F6 in the hex tab, take the 246 it returns, and subtract 256 to read it as -10.

Bases side by side, 0 to 16

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

Binary is padded with leading zeros to four digits so each row lines up with one hex digit (4 bits). The converter itself drops leading zeros and shows values such as 101 or 1 0000.

Sign notation vs. 8-bit two's complement

DecimalThis converter's binary8-bit two's complementHex (8-bit)
127111 11110111 11117F
110000 000101
000000 000000
-1-11111 1111FF
-10-10101111 0110F6
-100-110 01001001 11009C
-128-1000 00001000 000080

A signed 8-bit integer ranges from -128 to 127. To get the two's complement of -x, enter 256 − x into this converter as a decimal number. Entering 128 gives 1000 0000, which reads as 128 unsigned and as -128 signed.

FAQ

Can I type a prefix like 0xFF or 0b1010?
Can it convert fractions like 0.625 or 3.14?
Why do negative numbers get a minus sign instead of a string of 1s?
Can I paste values with spaces or separators in them?
How many digits can it handle?
Why is binary shortened to hex or octal rather than decimal?

Three ways to convert by hand

To go from base n to decimal, add up place values. Counting from the rightmost digit, the places are worth n⁰, n¹, n², and so on, so binary 1011 0110 is 128 + 32 + 16 + 4 + 2 = 182 and hex 2F is 2×16 + 15 = 47. In binary you only add the place values where a 1 appears.

To go from decimal to base n, collect remainders. Dividing 182 by 2 again and again leaves remainders of 0, 1, 1, 0, 1, 1, 0, 1, and reading them in reverse gives 10110110. In hex, 182 ÷ 16 = 11 remainder 6, and 11 is B, so the answer is B6. Keep dividing until the quotient reaches 0 and read the remainders from the bottom up; the same steps work for any base.

Between binary and hex or octal you group instead of dividing. Split from the right into fours and 1011 0110 becomes B6; split into threes and 10 110 110 becomes 266 in octal. If the leftmost group is short, pretend it has leading zeros. To go directly between octal and hex, pass through binary, which is the least error-prone route. Once the 0–15 rows of the table above are second nature, most conversions can be done in your head.

How computers store negative numbers: two's complement

A computer has to fit negative numbers into a fixed number of bits. Using the first bit purely as a sign creates both a +0 and a -0 and complicates the adder, so practically every modern processor uses two's complement instead. To build it, invert every bit and add 1. Ten in 8 bits is 0000 1010; inverted it becomes 1111 0101, and adding 1 gives 1111 0110, which is -10. The result is the same as writing 256 − 10 = 246 in binary, which is why entering 2ⁿ − x is the quickest route with this tool.

An n-bit two's complement value ranges from -2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: -128 to 127 for 8 bits, -32,768 to 32,767 for 16 bits, and -2,147,483,648 to 2,147,483,647 for 32 bits. Go past the top and the value wraps around, so adding 1 to 127 in 8 bits produces 1000 0000, which reads as -128. The bits 1111 0110 are 246 as an unsigned integer and -10 as a signed one; the bits carry no sign of their own, and the data type you read them as decides what they mean.

Widening a value copies its sign bit into the new positions, a step called sign extension. The 8-bit -10 (F6) becomes FFF6 in 16 bits and FFFFFFF6 in 32 bits, and it is the same reason -1 is all Fs at any width. When a debugger or packet dump shows a large value starting with FF, suspect a small negative number before you assume a huge positive one.

Where hex and octal show up in practice

Color codes are where most people meet hex first. #RRGGBB writes 256 levels per channel as two hex digits, for 256³ = 16,777,216 possible colors. The three-digit form #RGB doubles each digit, so #F80 equals #FF8800, and in the eight-digit form #RRGGBBAA the last two digits are opacity. An alpha of 80 is 128 in decimal, or roughly 50 percent opaque.

Permissions and bit masks are the other classic use. Each digit of a chmod mode is a bundle of three on/off switches, and programs that pack several settings into one integer use values with a single bit set, such as 0x01, 0x02, and 0x04. ANDing with a mask like 0xFF00 keeps only the bits you want, so 0x1234 AND 0xFF00 is 0x1200. A subnet mask of 255.255.255.0 is, in binary, 1111 1111 three times followed by 0000 0000, meaning the first 24 bits are the network part, hence /24.

Characters and hardware addresses are written in hex too. Unicode U+AC00 is 44,032 in decimal, the code point of the Korean syllable 가, and the 11,172 precomposed Hangul syllables run from U+AC00 to U+D7A3. A network card's MAC address is 48 bits written as 12 hex digits, and an IPv6 address is 128 bits written as 32.

Sources