Free tool

Number Base Converter

Convert whole numbers between binary, octal, decimal and hexadecimal with the place-value working shown, then switch to two's complement to see how 8-, 16- and 32-bit signed integers are stored.

Built for Intro Computer Science

The number

Whole numbers from 0 to 4,294,967,295 (32 bits). Prefixes 0b, 0o and 0x are accepted.

Result

Enter a number.

Number systems are the base layer under everything in an intro CS course. Encodr spaces out the conversions and the Python behavior built on them, so they stay fresh until the exam.

Get started free

How base conversion works

Every positional number system works the same way: each digit is multiplied by its place value, a power of the base, and the products are added. In binary the places are 1, 2, 4, 8, 16 and so on; in hexadecimal they are 1, 16, 256 and so on. To go the other way, divide by the new base repeatedly and read the remainders from last to first.

Hexadecimal uses the digits 0 to 9 and then A to F for the values 10 to 15. Because 16 is 2 to the 4th power, one hex digit stands for exactly four bits, which is why programmers write binary in hex.

Two's complement

Two's complement is the usual way to store a signed integer in a fixed number of bits. The top bit is the sign bit. For a positive number, write it in binary and pad with zeros on the left. For a negative number, write its magnitude in binary at the full width, invert every bit, then add 1. An n-bit value can hold -2n-1 to 2n-1 - 1, so 8 bits holds -128 to 127.

Worked example: the 8-bit pattern 11010110. Read as an unsigned number it is 128 + 64 + 16 + 4 + 2 = 214, which is D6 in hex and 326 in octal. Read as a two's complement value the sign bit is 1, so it is negative: 214 - 256 = -42. Check by encoding -42: 42 is 00101010, inverting gives 11010101, and adding 1 gives 11010110.

Binary is also why Python floats misbehave. A float is stored in binary, and 0.1 is an infinitely repeating fraction in base 2, so the stored value is only very close. That is why 0.1 + 0.2 == 0.3 is False. The details are in binary, hex and two's complement, and the course is in the free Intro Computer Science deck.

From the blog

Binary, Hex and Two's Complement Explained

Binary, hexadecimal and two's complement explained: place values, converting between bases, signed integers, and why 0.1 + 0.2 is not 0.3 in Python.

Read →

What's in Intro to Computer Science: 16 Units in Python

What an intro to computer science course in Python covers, unit by unit: types, loops, functions, lists, dictionaries, classes, recursion, exceptions and Big-O.

Read →

How to Study for Intro to Computer Science (CS1)

How to study for intro to computer science: trace code by hand, learn the Python traps exams test, drill exceptions and Big-O, and practice coding every week.

Read →

Big-O Notation Explained for Beginners, With Python

Big-O notation explained for beginners: what it measures, the five common classes, and how to classify loops, searches and sorts, with Python you can run.

Read →

Related tools

Subnet and CIDR Calculator

Get the mask, network and broadcast address, host range and usable host count for any IPv4 address and prefix - built for Security+ exam prep.

Open tool →

Security Risk Calculator (SLE, ALE, ARO)

Calculate single loss expectancy, annualized loss expectancy and the net benefit of a security control from asset value, exposure factor and rate of occurrence.

Open tool →

Coinsurance Penalty Calculator

See whether a property policy met its coinsurance requirement and exactly what it pays on a loss - built for P&C license exam prep.

Open tool →

Health Insurance Cost Sharing Calculator

Split a medical bill between you and the plan through the deductible, coinsurance, copay, and out-of-pocket maximum, step by step.

Open tool →

Real Estate Commission Calculator

Split a real estate commission between the listing and buyer brokerages, then between each broker and agent, with the math shown.

Open tool →

All free tools