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