Hello everyone, welcome to watch my video. In this video, I will tell you how to convert bytes to bits
First, you need to know that one byte is composed of eight bits. For example, for binary character
b8, its decimal unicode compound is 104, and in binary system, it is 01101000, which equals
64 plus 32 plus 8 equals 104 in decimal system. So that what we want to show you is how to get
the binary value 01101000 of the binary string b8. First, we will use a bitwise operation
Let's see this function. Use bitwise operations. This function takes a sequence of bytes as input
and I will call this function in the script method. We will set a breakpoint here
and divide to run. Click the run, start dividing. Now it will invoke the use bitwise operations
And it comes to this code line. This code line initializes an empty list for store bits
Then, it will iterate over each byte in the input sequence. For each byte, it will print the current byte for debugging purposes. We know the byte sequence is
binary 8, so its unicode compound is 104. It will print out this value. Then for each byte
we extract its bits. We use the method to iterate 8 times once for each bit in the byte
For one time, it will shift the byte to the right by the appropriate number of positions from 7 to 0. And then use the AND operation
Use the bitwise AND operation with 1 to extract 8 bits. This code will extract 8 bits. And then
it will call the append function of the bits list to append the extracted bits to the bits list
Yes, it will iterate 8 times. For each time, you can see the bits value. You can right-click
and click Add to watch for this value. Okay, now it will return the list of bits
Then it will print out the bits content. Yes, this is correct
Now I will show you the second method, useGeneralFormatting. This function's name is
useGeneralFormatting and it also takes a sequence of byte-selecting codes. I will call this function
in the run method. Call this line and then call this line. Okay
Okay. Debug it. Run. Start debugging. Yes, it will stop here and we will step into it. Okay
The first line iterates the empty list to store the binary string representation of each byte
Then it will iterate over each byte in the input sequence. For each byte, format the byte as an 8-bit binary number. Use fstr, and store it as a str
Yes, byte is a variable name of this one. And 08c means it has 8 numbers. Okay
And f means this is fstr. Yes, you can see this fstr is got the binary value. Okay. And then append the binary str to the list
And then draw all the binary strs together into a single str. Then return the str of bits
Print it out. Yes, this is also correct. Now I will show the third method. The third method
you use the bitarray library. To use bitarray library, you need to install it first
You can click terminal, no terminal. And then run the command tip show bitarray
to check whether bitarray is installed or not. If it is not installed, you can run the command tip
install bitarray to install it, because I have installed it already
And then you can import the bitarray class from the bitarray library
Now we will debug this function also. Let's debug from here. Click run, start debugging
Yes, start debugging. First you should import the bitarray class from the bitarray library
And then initialize the empty bitarray object. Then it will populate the bitarray with bits
from the byte sequence. The bitarray object from byte method will populate the bitarray with bits
And at last it will convert the bitarray to a list of bits and retain
Yes, this is also correct. This is for one character. I can show you another example for
multiple characters. We will not debug it, we will run it. Click run Python
Yes, this is the bitarray value of the string. If you do not want to get a list
you want to get a string, you can use this one. Okay, if you want to get a source code, you can go to this URL to get the source code of this
example video. That's all for this video. You're welcome to subscribe to my channel and I will
create more Python programming videos for you. Okay, bye-bye