Why does 🙂 take up four times as many bytes as A in an Irish Rail message?
You add a smiling face to a message about your Irish Rail trip. It looks like a single symbol, just like the letter A. So why does the smile take up four times as many bytes as A?
Unicode assigns a number called a code point to each character it encodes. For capital A, you see U plus zero zero four one. The digits after U plus give the number in hexadecimal.
Start with Greek lowercase alpha. Its code point is U plus zero three B one. Follow the arrow through UTF-8, which specifies how to store that code point. At the end you get two bytes, C E followed by B one.
You see C E followed by B one in a UTF-8 byte listing for alpha. Each pair of hexadecimal digits represents one byte. How many bytes does this sequence contain? Pick an answer.
Read each row from left to right. For A, both UTF-8 and ASCII give the byte four one in hexadecimal. Alpha takes two bytes in UTF-8, and the smile takes four. Neither has an ASCII code. When you and your receiver both use UTF-8, you interpret the bytes the same way.
Look up A, alpha and the smiling face in the UTF-8 column, in that order. Copy the bytes from each row, then join the sequences without rearranging any bytes. Count one byte for A, two for alpha and four for the face. That gives seven bytes.
This time your message starts with alpha, then has capital A, and ends with another alpha. Use the same table. Which byte sequence keeps the characters in that order? Pick an answer.
Start at the left and use the UTF-8 column in reverse. F zero, nine F, nine nine, eight two matches the smile. The next byte, four one, matches A. The last two, C E and B one, match alpha. Write the smile, A and alpha in that order.
Look at A and the smile side by side. Each looks like one symbol on screen, so counting symbols makes them seem the same size. Now compare the encodings. A has one pair of hexadecimal digits, and the smile has four pairs.
A Leaving Cert question might ask you to use the table to calculate how many bytes you need to store A followed by two smiling faces in UTF-8. Show your working. Count one byte for A and four bytes for each face. That gives nine bytes.