BrandGhost

Demystifying Base64 Encoding in C# - Beginner's DotNet Guide

Wondering how you can represent binary data in a human-readable way? We can use Base64 encoding in C# in order to convert bytes into a string! This is used in many places in computing, and it's only a few lines of code in dotnet! Have you subscribed to my weekly newsletter yet? A 5-minute read every weekend, right to your inbox, so you can start your weekend learning off strong: https://subscribe.devleader.ca Check out all of my courses: https://devleader.ca/courses Check out more Dev Leader...
View Transcript
in this video we're going to look at base 64 encoding in C this is going to be a really quick tutorial but I just wanted to explain situations where base 64 encoding could be helpful and how to go do it because it's really straightforward in C before I dive in just a quick reminder to check that pin comment for my newsletter well what is BAS 64 encoding and why should we care well we know in programming when we want to show text or represent readable characters we have strings to work with and a lot of the times when we're thinking about things like HTTP requests and other stuff like that we're able to just work with strings and it's really straightforward but there's other times where we want to be able to take a string and serialize it into binary data in those situations we can take an encoding like utf8 and then encode the string into bytes but what about the situations where you need a little bit of both what happens when you want to take binary data because it's representing something that isn't just a string and you need to transfer it as a string it's almost the exact opposite problem well that's where base 64 encoding can come into play so the way base 64 encoding works is that we're able to take binary data so that means taking single bytes that represent the range from 0 to 255 so 256 different representations and we can take arrays of that and have them as human readable characters and where base 64 comes from the 64 part is that we have 64 different characters that we can map that binary data to so it's unlikely that you'd want to go send your friend a picture but you want to have it in human readable characters like that would seem kind of silly however if you were working with a text based protocol and you needed to be able to take the bites of the picture and have that represented as human readable characters then converting that picture to Bas 64 could be totally useful in that situation so now that you have a rough idea of what we're talking about let's jump over to visual studio and we'll see how you can leverage base 64 encoding we're going to go from a string to bytes to a b 64 string and then we're going to go all the way back and see how that works so let's go check it out all right on my screen here I have something really simple that's just going to start by reading some input and putting that into a variable here so on line three we'll take whatever's on the console and we'll stick that into the variable that's called input from there we're going to get the binary representation of input so we can use the utf8 encoding right here and then get the bytes for the input and we'll store that into this variable called bytes so at this point we have a string called input and we have a BTE array called bytes but if we want to be able to work with base 64 encoding and have that binary data represented as a string again what's the next step well it's really simple actually on line seven you can see this one simple call that we make to the convert static class and it has a method called 2 base 64 string we're able to pass in that bite array and then the result of that is going to be yet another string but it's going to be those bytes encoded as Bas 64 let's go ahead and have this printed to the console and see what happens when we put some information into our program in order to make this program a little bit more interesting I'm going to put a while loop around this so we can keep testing some input so that was really cool I ended up typing while true above all of the code and copilot actually just rewrote the entire program for me pretty awesome let's go run it all right now that our program's running we can see that it says enter a string to encode so I will put Dev leader and we'll see what happens when I press enter and we get the encoded string so the base 64 encoded string that we see here is right here and you can see that it's kind of interesting because this is not the same string at all what's another interesting characteristic about this and I want you to pay close attention to this string that I have highlighted and what the input was which was Dev leader well one characteristic that sticks out to me is of course that they're both human readable characters in fact we don't have any weird symbols or anything else coming up the strangest thing that we kind of to see here are some equal signs these aren't really alpha numeric but there are a couple of extra symbols but there's still nothing weird like boxes that sometimes show up when you don't have the right font or encoding kind of going on so the they're human readable and like I said it's base 64 so the idea behind that is it's going to be um alpha numeric only with a couple of extra symbols like equals plus signs and slashes but one other interesting characteristic about this is the length so let's look at the length of Dev lead here which I have highlighted and this font is of course monospace so if I highlight this you can see that it's a little bit longer than Dev leader and that's going to be a characteristic in general of Bas 64 encoding that you're on average going to have about a 4 over3 multiplier on the length of what you're trying to encode and that's something to keep in mind because if Space is really important to you then base 64 encoding is going to inflate things a little bit it might not be the best option let's put in a really similar string and see what the output looks like all right if we compare Dev leader with the number two on the end it's very similar actually and what I wanted to point out about this is that if we're thinking about something that's cryptographic if you change a single bit in something that's truly cryptographic the entire representation is going to change even if one bit is different now with base 64 encoding it's not a cryptographic encoding all that we're doing is converting the bytes that are representing the string Dev leader 2 in this case and that's why if you look at the beginning of this what I have highlighted up to C it's identical to what we have in the first case up here what ends up changing is because the last character when we add the two onto the end we're going to end up having a different character representation right at the end because it's a different set of bits right at the end the reason I wanted to call this out is because if you're thinking about writing some of your first programs and you need to be able to store some things like encoding things to to base 64 is not some secret it's not a good way to encrypt things and hide things so please don't think that you're going to be able to put something to base 64 and not get it back in fact let's go look at that right now in terms of how we can recover stuff from Bas 64 we'll go the opposite way so because I don't want to think about trying to memorize or copy and paste some Bas 64 let's just extend this a little bit more so I'm just using co-pilot here and pressing enter and tab a few times and let's see if it fig out what we need to write so we'll get the input brought back in and then from there it looks like it can't figure out exactly what I want to do but that's okay so we'll put in a base 64 string and then we're going to use the opposite of this convert to base 64 string looks like co-pilot knows how to go the other way when you give it a bit of a hint all right so the way that our program is set up now once we encode something we'll have the opportunity to decode it right after so what I'm going to be able to do is highlight copy and paste what we just encoded and we should be able to recover it exactly as it was and you can see on my screen here on line 10 2 base 64 string is essentially the opposite of what's on line 18 from base 64 string and one of the reasons that I'm pointing this out to you is because when we're talking about converting between types in this case we're talking about going from strings to Binary representation and I wanted to point out that there is full resolution in this conversion so that means that you can take any binary data put it to base 64 and go back from base 64 to Binary data and you will never lose resolution in your data and what I mean by that is that because we're mapping between two different uh ranges of possible data you're not going to be in a position where it's lossy so you end up going one way trying to recover it and all of a sudden there's not enough resolution to get the full message back an example of something that's lossy is maybe doing something like image compression where you end up losing some of the data that represents your picture that's why you end up having fewer bytes but to get it fully back to normal that data is gone so it's going to be lossy there is lossless compression as well but in this case when we're talking about this conversion this is a lossless conversion of data let's go prove it all right let's try again with Dev leader put into here and we should get a familiar base 64 string I'm going to go ahead and copy that so now that it's copied and on my clipboard once I paste it in let's go decode it and hopefully we get Dev leader back exactly wait a second what went wrong here we missed one step and if you were paying attention you would have caught this but when we go from base 64 to a bite array the thing is that that's not printable yet there's one more step and that's because in this conversation we're essentially going string bite array string it's just that one of those versions of the string is a base 64 encoded string so we're missing one step because we've gone to a bite array now but we have to go to a human readable string that's not base 64 so let's go back to the code and see what step we're missing all right so if we look in the code here this decoded variable that we have is actually a BTE array and what we need to have is a string so if we look back up here on line 8 we can see that this is where we're doing something with the utf8 encoding and in fact we want to be able to do that on on this decoded BTE array so again we're going from a string here putting that into a bite array and then we need to go from a bite array back to a string so let's go try putting the opposite of utf8 get bytes all right with that one extra line added in we are getting the string from the decoded bite array and we get utf8 decoded containing that string now so if we print that to the console this should work right now we have this on our clipboard and I will paste it right in here and let's get a drum roll going boom we get right back to Dev leader so just to reiterate because these are both string representations we need to be able to go from one string to a bite array and back to a string it's just that one of these has the extra step of the base 64 encoding all right well that's going to wrap up this video on base 64 encoding in C as you can see it's only three lines of code to be able to get some string input to convert that into a b array and then from there into a base 64 string keep in mind that when you're going from binary data to a base 64 string there is going to be an inflation in size it's about a 4 over3 ratio of the size that you'll have in the end so where might you be using Bas 64 encoding feel free to sound off in the comments thanks and I'll see you next time

Frequently Asked Questions

What is Base64 encoding and why is it useful?

Base64 encoding is a method of converting binary data into a string format using 64 different characters. It's useful when you need to represent binary data, like images or files, in a text-based format, such as when sending data over HTTP requests.

Can I recover the original data after encoding it in Base64?

Yes, you can recover the original data after encoding it in Base64. The conversion process is lossless, meaning you can take any binary data, encode it to Base64, and then decode it back to its original form without losing any information.

Does Base64 encoding increase the size of the data?

Yes, Base64 encoding does increase the size of the data. On average, you can expect about a 4/3 increase in size when converting binary data to Base64, so it's something to keep in mind if space is a concern.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload