Base643:08 with Jason Seifer
From time to time in your programming career, you'll need to deal with binary encoding. One of the many ways to encode binary data to text is using the base 64 encoding scheme. A binary encoding scheme will represent binary data in a text format. We mainly use this when sending binary data across a medium which expects text data, like when requesting web sites across a network. There are severan encoding formats available for this and, programmatically, many function the same way.
From time to time in your programming career, 0:01 you'll need to deal with binary encoding. 0:03 One of the many ways to encode binary data to text is using 0:07 the base 64 encoding scheme. 0:12 A binary encoding scheme will represent binary data in a text format. 0:15 We mainly use this when sending binary data across a medium which 0:21 expects text data, like when requesting websites across a network. 0:26 There are several encoding formats available for this, and 0:32 programmatically, many function the same way. 0:35 We're going to take a look now at Base64. 0:39 Let's load up a work space and get to it. 0:42 So, the next thing we'll take a look at is the Base64 module. 0:46 Now, sometimes we'll be in the situation where we need to encode and 0:50 decode binary data. 0:54 This is a little bit more common when we're working with web based programs. 0:56 It's a great method of sending data back and forth and 1:00 making sure it stays in the same place. 1:03 Now we get a couple of different methods, decode and encode 64, 1:06 with a couple of different variants, first strict and URL safe. 1:11 Now, let's go ahead and just see how this works. 1:16 So I'm gonna load up a work space, and I've loaded up IRB. 1:19 Now we have to acquire Base64, 1:23 and we'll say our message is Welcome to Treehouse. 1:26 Now if we wanted to encode this message, we could say Base64.encode64 and 1:33 then pass in our message as the argument. 1:39 And we get back this encoded string. 1:43 Now the same way, that we encode it. 1:46 We've got this encoded string now. 1:55 We could decode it as well by using the decode64 method. 1:58 And we get back our original string. 2:05 Now the other variance that are in use are strict decode and strict encoding. 2:08 No line feeds are added. 2:14 Now we saw that at the end of the sting that was encoded. 2:16 If we go back here, we can see we've got new lines added here. 2:20 And there's also a variant that does URL safe encoding and decoding. 2:24 Which changes the dash instead of plus and then underscore instead of slash. 2:30 This makes it a lot more useful if you are posting webpage data. 2:36 But those are the two methods that we use. 2:42 So let's see Base64.encode64(message). 2:44 And now we can also do urlsafe_encode64. 2:51 And we get a very slightly different string, just without that \n. 2:59 And the decode64 would work the same way as well. 3:05
You need to sign up for Treehouse in order to download course files.Sign up