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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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.

Comments & Discussion

Eric Koston Jr on July 22, 2020

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 on July 23, 2020

Hi Eric,

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

missing-link on Aug. 4, 2020

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 on Aug. 4, 2020

Hi missing-link,

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.

Become a Member to join the conversation.