I am now going to throw you a bit in the deep end. We're going to be discussing the accept-charset.
And all this is, is it's a form attribute.
So, it pretty much looks like this.
And here you can see we've set the accept-charset to "utf8"...
and I'm going to be discussing exactly what this means, shortly.
But I want you to be aware that this is quite an advanced topic.
And I'm not joking when I say I'm pretty much throwing you in the deep end, and it may feel like it's...
a bit too deep for so early on in the course, but I'm here for you.
I'm going to get it through...
and I've tried to lay things out as simply as I can.
And very few developers even know this.
If you ask a developer what the accept-charset is, yes, they'll probably say, listen, it's it's a type...
of encoding...
but very few understand the true meaning of utf8...
and why differs from other types, such as ASCII, for example.
Do you know what I've just thought of?
I just thought of that old song, um, "Sum 41 - in too deep".
Do you remember that song?
Let me remind you.
(music by Sum 41)
Oh, brilliant... it reminds me of the old times.
But anyway, the point is, I'm throwing you in the deep end, but don't worry, we're going to get...
through it together. Okay,
where do we start?
Well, in order to understand the accept-charset attribute, we need to have a geek 🤓 lesson, because...
it really requires you to understand how we treat text on computers.
So let's have a look.
You need to understand that when we have text, and let's just literally write the word "text" to the screen,
that's just a sequence of characters.
And when we want to either store it inside a computer, aka. the machine, or if we want to transfer...
it over a digital network, for example, like submitting it to a server, we need to convert this text...
into something the machine can understand - into a binary representation, because that's the only language...
a binary-based computer can understand.
And you and I know that our computers, at its core, can only understand 1s and 0s. In fact, it can only...
understand electrical inputs - either on or off.
And how do we convert this text into binary?
We do this by using character encoding.
And just remember, "encoding", that specific word, just means converting data from one format to another.
Does that makes sense?
So "character encoding" specifically, that just means it's a way of converting text data into binary...
numbers.
In a nutshell, we can assign unique numeric values to specific characters and convert those numbers...
into binary language.
And these binary numbers can later be converted back to the original characters based on their values.
That's all that "character encoding" means.
And can you see that word, "charset"?
That charset...
is just short for "character set" and a character...
set, is just the set of characters that are allowed to be encoded.
As we'll see shortly, the ASCII encoding encompasses a character set of 128 characters.
UTF-8 has a ton more. But let me not get ahead of myself.
So moving on, we know the accept-charset attribute specifies the character encoding type, how...
those characters are going to get encoded into binary data. The default value of accept-charset,
for example, when you don't even include it on your form, is the reserved string "UNKNOWN".
And this indicates that the encoding equals the encoding of the document containing your form element.
Pretty simple, and most of the time you don't even need to include this attribute because by default...
your entire HTML file is set to UTF-8.
But I digress...
let me get back onto topic.
Over the years, people have developed different ways to map characters to 0s and 1s.
The most common, though, are these three: (1) ISO (2) ASCII and (3) UTF-8.
Hang on, how do I know what the encoding type is on any given webpage that I visit?
Well, let me just jump over to a random web page now and I'll show you how you can see it.
OK, so here I am, I'm just in incognito 🤺.
And as you'll see, most browsers today, as I mentioned, use UTF-8. But how do we see what the...
encoding type is for...
our document? Well, remember a few lectures ago, I said that many attributes are also properties on...
their object.
Well, the charset can be accessed directly from the document, like this.
So, how do we do it?
Well, it's very simple. Let's just go to the console.
And we can just access this attribute directly as a property.
Remember when we were discussing attributes a bit earlier, we mentioned how important it is to know...
what attributes are attached to elements, because those attributes in many cases are converted into...
properties of an object.
And this is one example why its really useful.
All we have to do is access our "document"...
and on this document, we literally have the charset property.
And as we can see here, the character set on this Google page is UTF-8.
How awesome.
But now, let's jump back into the lecture.
Does that make sense?
Hope it's starting to feel more intuitive.
It's not that difficult.
But let's now discuss each one of these three common encoding types in a little bit more detail.
So let's start off with ISO.
Firstly, I just want to point out that this ISO standard is a legacy standard dating all the way back...
to the 1980s - old-school.
It is similar, kind of, to ASCII and it can only represent 256 characters...
so it's only suitable for some languages in the Western world.
But even for many supported languages, some characters are missing. If you create a text file in this encoding...
and try copy / paste some Chinese characters, for example, you're going to see weird results 🥴.
And because it's so old-school, I just don't want us to even discuss this anymore.
So let's move on. The next common type...
is this ASCII character encoding set, and this contains a total of 128 characters and each character...
has a unique value between 0 and 127.
Of course, if you include the 0, that's why we have 128 characters in total. And because it only...
has 128 characters, a 7 bit...
binary number is sufficient to represent a character from the ASCII character set, since a 7...
bit number can hold values from 0 to 127.
Remember, a bit is just a 0 or 1.
That's all a bit is.
And if we have 7 bits, we can represent 128 unique values.
How do I know that?
Well, it's just simple math, but here we go.
Here's 7 bits on the screen, and you can literally have so many different combinations.
You can have all 0s.
You can put a 1 on the end.
And when you go through all the different combinations, you'll eventually end up with all 1s...
and that, my dear friends, is 128 unique values.
I wish it could be this simple though, and we could just stop here.
But actually what happens in the real world is that your typical computer system has memory. And memory...
is made of unit cells, and each individual cell contains 8 bits, not 7 bits ... 8 bits. And 8...
bits are called a "byte".
And this means that although ASCII only needs 7 bits to encode a character, it is stored in 8 bits.
And this just means we've got a redundant bit
right?
And by convention, the first bit is always 0, for that reason.
In fact, since the first bit of an ASCII characters is always 0, it's called a dead bit.
Let me show you what I mean.
So here we go.
Here's ASCII characters, and there are the bits that represent the letters A through to F.
Pretty simple. But you'll notice that the very first bit are dead bits.
They are always 0. So that's a very quick way for you and I to kind of spot the character encoding...
type...
whenever you see binary data like this. That's just a bit of FYI. You know what else I could show you,
I just thought of right now, why don't I just jump to Microsoft Word and I'll show you the ASCII character
set.
Let me show you.
I love the stuff, and I hope you're having a ton of fun like me.
But anyway, as I mentioned, I want to show you where we can see this ASCII character set.
All you have to do is click on insert.
And this is just Microsoft Word, by the way.
I don't know why...
I wanted to show you this example.
I just think it's interesting.
And we just can click on "Symbol", then "more symbols".
And look at that.
We've got Unicode here, which we don't want. I want to show you this ASCII character set. And here
they are.
You can literally scroll through all of them and there are not that many.
But, look here. If I click on the last one...
you'll see that there's a character code, and that has a number of 255.
255.
And if we include the character code "0" itself, we can see that there are 256 unique values.
Why is this 🤷🏻‍♂️?
I've just said that the ASCII character encoding set contains 128 characters and now I've just showed...
you that there are actually 256.
Well, don't get confused.
Microsoft Word uses the "extended" ASCII character set.
Remember, we had that dead byte? That byte that's just 0.
Well, the extended character set uses that dead byte to store other characters, and that gives us an additional...
128 characters, which is why we get this.
But anyway, putting that aside, just understand, this is the ASCII character set.
I just wanted to show you what it looks like, and where you can even see some of these things, because
it can be very daunting when you first start out.
Enough said, let's hop back into the lecture.
This is quite interesting, isn't it?
Hope you're learning a ton. I hope its starting to become a "bit" (no pun intended) more intuitive now.
The ASCII character set is just a group of characters that are part of that set.
And very clever people in the background have mapped those characters in order to be converted into binary...
data.
Quite cool.
But now the question you're probably having is, "Clyde, are 128 unique values, enough?"
Well, let me say that they are 95 human readable characters specified in the ASCII table. Letters A through
to Z, both uppercase and lowercase...
the numbers 0-9, a handful of punctuation marks and characters like the $, and the ampersand...
and a bunch of others.
It also includes 33 values for other things like spaces, tabs, backspaces, and a few more. Notice that...
those weird characters are not printable per say, but they're still visible in some form and useful...
for humans.
So, in total there are 128 characters in ASCII encoding type, which is a nice round number.
But let's just be honest, 95 readable characters just doesn't cut it.
There's no spec on how to represent emojis, for example 😱.
Or what about Chinese?
Well, if we only had the ASCII character set, we would not be allowed to ever write Chinese or include emojis...
on a computer screen 😡.
You can really tell how average that would be.
So to cut a long story short, we need more! We need more characters. Whew...
but because this lecture is already going on quite long, I want to stop here.
And in the next lecture, I want to blow your mind by discussing what exactly UTF-8 is.
I can't wait.
See you now.
(music)