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.
Working in Binary: Bits, Bytes, Oct, and Hex
In the previous lesson I showed you the Python
string module and the ASCII constants inside of it. In order to understand Unicode, you need to understand a little bit more about byte-wise representation, so in this lesson, I’ll be talking about bits, bytes, octal, and hex notations.
Or, if you come at it from the right-hand side, 4 * 10^0 + 3 * 10^1 + 2 * 10^2 + 1 * 10^3. Now, why did I break it down this way? Well, because if you change the base, the same concept works, but instead of powers of 10, you switch to the powers of the base. In binary, the power is 2. To type a binary number in Python, you can prefix it with
0b. Here’s an example.
0b1001 is the binary number 1001. Like before, you can start on the right-hand side using powers of 2. 2^0, which is 1—in this case, 1 * 1. Then, 2^1—but we’re skipping this digit. 2^2—skipping this digit as well.
01:25 2^3 is 8. So, the two parts of this number that have a 1 are 2^3 and 2^0, which is 8 + 1, giving you 9 in decimal. In computer science, you often run into powers of 2, powers of 8, the normal decimal, and powers of 16. To show the differences here, let’s examine the decimal number 539 represented in the different bases.
You can’t use 10 because in hex, that would mean 16 + 0, so instead, letters are used.
A is decimal 10,
B is 11, et cetera. So on the right-hand side,
B turns into 11 * 16^0 + 1 * 16^1 + 2 * 16^2, for a total of 539 in decimal.
03:47 The three digits on the left easily map to three groups of 4 bits on the right-hand side. 4 bits, or half a byte, is called a nibble. Each nibble becomes a hex digit. Converting in the other direction is just as easy.
539, putting it in a variable name
number. For starters, I’m going to convert this into text using the f-string. No real surprise there—
'539'. Using the format option of f-string, it can be converted to other representations.
You can also pass in a base. Now note, what’s happening here is not converting decimal
539 into hex, but representing the hex number
'539' and turning it into an
int, which by default, is a decimal. So
'539' hex is
You can do this with binary as well, but of course, 5, 3, and 9 are not valid digits in binary, so you get an exception. 1 and 1 are valid base-2 numbers, so binary
'11' gets converted into
This little snippet of code is handy to make binary a little more readable. It converts
539, into a
str (string) and then iterates over it digit by digit. Using an f-string,
ord() gives the code point of that digit and presents it in binary, thus giving you byte-by-byte chunks of the three digits 539.
Become a Member to join the conversation.