 Welcome back everyone. This is Brian. This is episode 25 working with binary files. So first things first What is binary? We've worked with plain text, but what's binary? It sounds kind of crazy almost like something from the matrix The harsh reality is binary is something you're really not going to want to work with directly and I'll explain why Go over to the extensions tab and let's install a hex editor Now spoiler alert you do not need this specific hex editor. You don't even need this IDE. I'm running VS code You can just use any old hex editor out there hex is hex does not matter as long as you can view the actual hex representation here Once that's installed we're gonna flip back over to the files and We're going to take this file that we're in We're going to right-click and And then from here, we're gonna go open with the hex editor. We literally just installed you can see we have the Hex encoded bytes along with the decoded text. This is what I'm talking about here decoded This is what the computer works with. This is what the machine reads This over here is what we usually work with the plain text So for example pound is hex 23 then you have 57, which is a capital W 6 f I mean imagine trying to type out just like your name in hex That would be long and frustrating So when we're talking about binary We're talking about the raw data the computer works with it's not something we want to work with directly We want to work with a representation of that data So that's really what we're talking about when we say binaries. We want to give that computer a Representation of binary data and let the computer do all the hard work for us. We're not working with plain text We can work with individual bytes that represent plain text, but we're not working with the plain text itself All right, we need to leave binary land here and we're gonna go back to plain text land And this is just the file. We've been working with gonna pop in some notes and It's gonna just say install the Microsoft hex editor extension don't really need it to be the Microsoft one But then some general instructions on how to work with it in case you're following along with these videos Now we're gonna add some imports and we need to do this step before we even continue with anything else So I'm going to import random and What I mean by random is we're not just randomly importing something we're importing a module named random which will help us create random numbers And we're going to import the operator module This has a little function here called equals or eq that we're going to use to compare two lists together just to make sure that the Information we're generating and the information we're saving and loading all matches up and everything works as expected Let's go ahead and work with that random module and we are going to create Some random bytes here. So let's make a function. Let's call this random bytes And from here We're going to just have a parameter called size. Let's go ahead and make a list Absolutely nothing in that list now. We're going to say for x and range And we want a range to equal the size We're getting that from our argument Let's go ahead and append our list so bytes And Now we want to actually say random And we want a range. So I'm going to say random range And if you know anything about a byte It typically goes from zero to 55 You can do some crazy things and we're just going to stick with the norm zero to 255 here Very simple easy for anybody to understand. We're not talking about some Crazy encode encoding schemes or anything like that just zero to 255. That's all we want From there We're going to go ahead and return The bytes so we're just going to return that list So random here is going to make a random number within a range Between zero and 255 so we're going to have up to 256 possible numbers Now random you can do seeds and all this other fancy stuff and then we're not doing any of that So what it's going to do is it's going to take the current system time and use that as a random number Seed if you wanted something super secure, you would have to do something different But we're just going to leave everything as default for now Now let's go ahead and print that out just to make sure we are getting well, you guessed it some sort of random list of numbers so say def and display bytes Go ahead and we're going to introduce a few concepts in this video here some things we really haven't talked about So i'm going to just make a string now. I want to multiply this so we're going to say times 20 That looks a little funny, but what we're going to do is we're going to say take this string and do it 20 times So we're saying this string times 20 Now we're going to go ahead and rent out Our little list of bytes here to do this. We're going to use the enumerate function. So I'm going to say four index comma item in Enumerate and what enumerates going to do is it's going to pull that whole object apart Into multiple pieces things that we can use and you see where it says right there enumerate object yields pairs containing a count from start which defaults to zero So we're going to tell it to take that bytes list and enumerate it and we don't want to start at zero we want to start At one because people are not computers. We'd like to start at one not zero We're just going to make this, you know human readable Now we're going to go ahead and format out a print and let's say index equals And we want the item Now we've been working With a hex editor. So I want to show you a simple little trick here. We can just simply say hex And we want to make a X representation of this item so that we could later compare it to a hex editor and make sure that everything Lines up exactly the way we would expect it to Gonna grab that little print function drop back down Make sure that lines up. So we're not doing it every single for loop. But after the for loop is completed And let's test this. So I'm going to just grab our function random bytes and Whoops Grab too much of that apparently There we go And let's just say 10 just for now. Nothing too crazy. Now. I want to go ahead and display That list Let's just see what this looks like So sure enough we are getting a list It is one through 10 human readable starting at one under the hood It's actually zero, but we're using this little start here to increment it and then we have the Decimal and the hex representations of that data Okay, deleted our little test there and we are going to now move ahead and we're going to write some bytes here Now we have to do this a little bit differently. We've been working with plain text and we're working with binary files This is not human readable. So we have to tell python. We don't want humans to work with this We want computers to do it So we're gonna say def write bytes We want a file name and the list of bytes Now that list is just going to be a list of integers. So it's a little misleading where it says bytes This function is going to convert those integers two bytes and store them in the file We're going to introduce something a little bit different here. It's going to be the width keyword So we're going to say width width is going to take some code Function that returns a variable And use the variable. So let's go ahead and demonstrate that So we're going to say width open. We're going to open the file name We need to give it a mode and we want to write binary if we just did w it's going to be plain text. So we want that b in there as file So what we're really doing here is we're saying width This function run this function Return a variable and call it file. So width Some function as file kind of reads like a book with this as this variable Now that we've got that variable We can just work with it directly I'm going to say for b in bytes And we can use that variable we got from our width statement file write And we're going to take that individual little number and I'm going to say to underscore bytes It's a little misleading that it's a b representing bytes. This is actually a list of integers So I very easily could have said I Doesn't really matter what we call it So we're going to convert that to bytes now we want to convert it to one byte And if you're A computer expert and you understand this we're going to give it a big order. So I'm going to say byte order Equals big You don't know what that is. Don't worry about it. We're just going to use this just for demonstration purposes It's really doesn't matter unless you're specifically want A big or something else All right, that's it. We're done. Now you may be going now. Wait a minute. We have a file object Don't we have to close it shouldn't we do something like file close? Well, no that would cause an error and that Would Not cause an error, but it's really not needed because width Is going to destroy this when it's done with it And when that variable is destroyed it's going to call close automatically So we're actually done It's you notice as we get more advanced in python even though we're writing more advanced code the code actually gets shorter and Shorter and shorter. This is one of the really cool things about python And we could shorten this even more if we wanted to but we're still in beginnerland So this is about as short as i'm going to make it for this video Now that we've written the bytes we want to do the exact opposite We want to read those bytes back and i'm going to intentionally make this function A little bit bigger than it needs to be just because we're still in beginnerland and I don't want to confuse anybody So i'm going to say def Read bytes and i'm i'm saying that because you'll see people down in the comments going well This is very verbose. Why are you doing this when you could do and they'll put a little one liner in there and everyone looking at It's going to just be like I don't understand what they're talking about So i'm going to say bytes we're going to make a blank list You're going to say width open And we're going to get our file name now This is the trick here We need to do the opposite. We're going to read bytes not read plain text And I call it bytes but it's read binary As file Now i'm going to do a loop and i'm going to say while true Loops are big and scary for noobs. So don't worry. We're going to tell this when to stop looping So it's not going to enter some infinite loop I'm going to say b equal file dot read and we want to read just one Now we're not reading the number one. We're just reading one byte from that file If not b and I think we've talked about not before it's the same thing as Like something like that not equals to but we're just saying not b And we're going to go ahead and break Break will break right out of that loop However, if we're still here in loop land, we want to say bytes Append Let's go ahead and take int From bytes. So we're doing the exact opposite here instead of two bytes. We're saying from bytes So we're taking a byte and converting it to an integer And it's pretty simple. We're just going to take our B that we read from the file and we're going to set the byte order if we really really want to So let's just do that. All right now Once we're in there, we're going to drop back down to this level right here and we are going to return Our finished list Looks big and scary, but really we're just doing the opposite of write bytes. We're just saying hey Make a blank list And then with open get a file And then as long as we can read one byte Append that to our list of integer and return it Looks big and scary, but it's actually pretty simple once you wrap your head around it Now that we've got all the pieces together. Let's see it in action This is what I love about programming. It is a lot like working with legos once you get the pieces together You can make something bigger and better So let's see it in action. The first thing we're going to do here is create the random bytes So I'm going to say out bytes. You can call this whatever you want. It doesn't really have to be out bytes And this is going to be our random bytes function And we're going to head and just let's just keep it at 10 I don't want anything too crazy because we're going to have to see it on the screen Let's go ahead and display that just so we can see The out bytes And I'm going to walk through this step by step here. So let's go ahead and run this And we've seen this before I've got this kind of split up here So every time it displays it'll have this nice neat little bracket there Now we're going to write all that to a file. So I'm going to say file path And let's call this test dot Bxt Now this will make some people mad because what we're doing is we're violating some fundamental concepts of the operating system People expect that a dot txt file is a plain text And I'm doing this For illustrative purposes to show you that the file name and extension have absolutely nothing to do with the data that goes inside them So now that we've got a file name, I'm going to go ahead and say write bytes And we can just simply take that file name Along with our out bytes And dump that to a file Let's go ahead and run this again Uh-oh file name is not defined. Oh, I've got file path. That's why Shockingly the variables have to line up. All right, let's try that again Clear that run it. Okay, it doesn't look like anything happened on the screen But over here now we have this little dot txt file And if I open it I get kind of these crazy characters Well, what is this? This looks like gibberish. Did we have a corrupt hard drive? No, let's go here Open with our hex editor and see what's going on here So if we kind of scroll up here You can see how we have One equals 84, but then we go into a panic and go it's 54. What's wrong? Well the hex version of 84 is 54 And then we have f1 f1 Or four you see what's going on here somebody out there's going to ask. Well, why does it say 0x? That's kind of like the universal representation that we're working with hex data So in your mind just kind of blank out that 0x and just pay attention to everything after x So we have 54 f1 4 e1 d3 and you can see it's just working as expected now This is what I mean by a lot of people kind of get into a panic when you tell them you have to work with binary data Because they don't really understand this and they think that they have some horribly bad screwed up computer Or that their program did not work well because they look at it and go Uh-oh, it says 54, but I should have put 84 and they try to figure out what's actually going on This is what I mean by we're not working with binary directly We're working with a representation of the binary data. We let the computer figure it out for us continuing on here Almost done now. We're just going to take that file and read it back in. So I'm going to say in bytes Equals and we're going to go ahead and read bytes And we're going to read that file Hey, there we go Once we have that we're going to go ahead and display it And now we need to go all the way back to the beginning of this video when we said we're going to use the operator Import and we're going to compare them So I'm going to say print And let's say match. I just want to know if they're a match kind of like what is that dating site match.com? It's a match All right, so I'm going to say operator dot ecu And now we need to feed it some lists So I want to say bytes and in bytes again doesn't really matter what you name these Now because we are using a random number generator That the seed is based on time these numbers will change every time I run this because time is constantly changing Just wanted you to be aware. So if you're expecting 84 241 4 blah blah blah you're in for a very big surprise here Or maybe not now that I've told you what's going to happen. Let's run this. See what happens here It says match true. So what happened here and let me Scroll this up We have our output In our input and of course they line up every single thing works correctly and the two lists now match So what we've done long-winded version of this Scroll all the way up here We've created a list of random integers. It says random bytes, but it's really integers Then we can display those and then we can pump those out to a file and then write them as bytes Then we can read them back and read those bytes into integers and put them back into a list and return that And we can actually check to make sure Everything matches. So what we wrote and what we read now match The big question is how would you modify this information? Well Let's say you wanted to do some sort of work You would do that to your outbytes before you wrote it to the file And all of your changes would be written to it See our file is right there and if we go into open with our hex editor and we get the nice representation of it I hope you enjoyed this video. You can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void realm's facebook group This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched And if you want official training, I do develop courses out on udemy.com This is official classroom style training if you go out there and the course you're looking for is just simply not there Drop me a note. I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button The more popular these videos become the more I'll create and publish out on youtube. Thank you for watching