Number base converter
Type a number in any base and see it in binary, octal, decimal and hexadecimal.
This tool runs in your browser. We never send what you type or paste to a server.
Bases in programming
- Binary (base 2): how computers store everything. Prefix
0bin many languages. - Octal (base 8): Unix permissions like 755. Prefix
0o(or a leading 0 in C). - Decimal (base 10): everyday numbers.
- Hexadecimal (base 16): colors, memory addresses and bytes. Two hex digits make one byte. Prefix
0x.
Numbers of any size are supported, using JavaScript's BigInt.
Example
decimal 255 4096
binary 11111111 1000000000000
octal 377 10000
hex FF 1000
Type in any box and the others update. Very large numbers are handled exactly, without the rounding you get from ordinary JavaScript numbers above 253.
How base conversion works
In base b, each digit is worth b times the digit to its right. So binary 1011 is 1×8 + 0×4 + 1×2 + 1×1 = 11, and hex 2F is 2×16 + 15 = 47. The converter reads your input this way, one digit at a time, into an exact BigInt, then writes it out in the other bases. To go the other way by hand, divide repeatedly by the target base and read the remainders from last to first: 47 ÷ 16 = 2 remainder 15 (F), so 47 is 2F.
Binary, octal and hex convert without arithmetic, because 8 and 16 are powers of 2. Each octal digit is exactly 3 bits and each hex digit exactly 4. Group the bits from the right: 1011 0110 is B6, and 10 110 110 is 266 in octal. This is why hex is the usual way to write bytes (two digits each) and why Unix permissions, which come in groups of three bits, are written in octal.
Negative numbers and two's complement
The converter keeps a minus sign as it is: −5 becomes −101 in binary and −5 in hex. That is the mathematical answer, but it is not how a computer stores −5. Signed integers use two's complement, where −5 in one byte is 11111011 (FB), the value you get from 256 − 5. So if you are decoding a byte from a hex dump, FB could mean 251 or −5 depending on the type, and the converter shows 251.
To get the two's complement form yourself, mask the negative number to the width you want (-5 & 0xFF in Python, -5 >>> 0 for 32 bits in JavaScript). To read a byte back as signed, subtract 256 when it is 128 or more.
Conversions in code
Python:
print(int('ff', 16), int('0b1010', 0), int('0o17', 0)) # → 255 10 15 base 0 reads the prefix
print(hex(255), oct(8), bin(5)) # → 0xff 0o10 0b101
print(format(255, '08b'), format(255, 'X')) # → 11111111 FF
print(bin(-5)) # → -0b101 a minus sign, not two's complement
print(format(-5 & 0xFF, '08b')) # → 11111011 -5 as an 8-bit two's complement byte
print(int('11111011', 2) - 256) # → -5
JavaScript:
console.log(parseInt('ff', 16), (255).toString(2), (255).toString(16)); // → 255 11111111 ff
console.log(Number('0x1F'), Number('0b101'), Number('0o17')); // → 31 5 15
console.log(parseInt('0x1F'), parseInt('0b101')); // → 31 0 parseInt knows 0x but not 0b
console.log(parseInt('12px', 10), Number('12px')); // → 12 NaN parseInt stops at the first bad digit
console.log(2 ** 53 + 1); // → 9007199254740992 past 2^53 numbers lose precision
console.log(BigInt('0x' + 'f'.repeat(20)).toString()); // → 1208925819614629174706175
console.log((-5 >>> 0).toString(16)); // → fffffffb -5 as a 32-bit two's complement value
Bash arithmetic and printf:
echo $(( 16#ff )) $(( 2#1010 )) $(( 8#17 )) # → 255 10 15
printf '%x %X %o\n' 255 255 8 # → ff FF 10
echo $(( 010 )) # → 8 a leading 0 means octal
echo $(( 10#010 )) # → 10 force decimal, e.g. for dates like 08
Gotchas worth knowing: parseInt silently stops at the first invalid character, so always pass the radix and check the whole string if the input matters. JavaScript numbers are exact only up to 253, so 64-bit IDs and hashes need BigInt. In Bash and in C, a leading zero means octal, which turns 08 and 09 (from dates, for example) into errors unless you write 10#08.
Questions
What is 255 in hex?
FF. It's 11111111 in binary, the largest value one byte can hold.
Can I paste numbers with 0x or 0b prefixes?
Yes. Prefixes, spaces and underscores are ignored.
Why is 0xFF equal to 255?
Hex digits run from 0 to F (15), so FF is 15 × 16 + 15 = 255. That is the largest value one byte can hold, which is why hex is so common for bytes and colours.
How do I convert binary to hexadecimal?
Split the binary digits into groups of four from the right, padding the leftmost group with zeros, and replace each group with one hex digit: 0000 is 0, 1001 is 9, 1010 is A and 1111 is F. So 101101 becomes 0010 1101, which is 2D.
Why does 0.1 not have an exact binary form?
Only fractions whose denominator is a power of two terminate in binary. One tenth is 1/10, so in binary it repeats forever, and a float stores the nearest value it can. The converter works with whole numbers only, so it does not show fractions.
How do I show negative numbers in hex as two's complement?
Mask the value to the bit width you need. In Python, format(-5 & 0xFF, "02X") gives FB for one byte; in JavaScript, (-5 >>> 0).toString(16) gives fffffffb for 32 bits. The converter itself keeps a minus sign instead.