Join us and get access to thousands of tutorials and a community of expert Pythonistas.

# Working in Binary: Bits, Bytes, Oct, and Hex

In this lesson, you’ll learn about bits, bytes, octal, and hex notations in Python. To better understand Unicode and UTF-8 encoding, you need to be familiar with binary and hexadecimal numbers. You’ll also learn about three literal forms for binary in Python: binary, octal, and hex literals.

Eric Koston Jr

There seems to be a typo when giving an example of converting binary to hex @4:02.

The last nibble is shown as 0b0101 which is said to convert to 0x9.

Doesn’t 0b0101 map to 0x5? Shouldn’t it be 0b1001?

Christopher Trudeau RP Team

Hi Eric,

Thanks so much for pointing that out. Good catch! We’re working on a fix and will get it up shortly.

Could somebody elaborate what’s going on with int(539, base=16)?

Is the rule equal 5x16^2 + 3x16^1 + 9x16^0 ?

Thank you

Christopher Trudeau RP Team

Yes, base=16 tells `int()` to treat the incoming string as if it is in base 16, also known as hexadecimal. You are correct. To convert hex into decimal you use the general math formula of:
SUM: `digit_value * 16 ^ digit_position - 1`
where `digit_position` starts at 1 and counts from the right hand side.