NCL 209: Unicode
In the previous lesson, we saw that an escape sequence such as:
S.MOVE #message "A\nB"
does not leave the two characters \ and n in the string.
The escape sequence is part of the source code. Once it has been interpreted, the string contains a line-feed character:
A
B
That raises a useful question.
How does a computer identify one character from another?
Characters as numbers
Characters can be identified using numbers.
For example:
| Character | Code |
|---|---|
A |
65 |
B |
66 |
a |
97 |
0 |
48 |
| line feed | 10 |
NCL can inspect these values using S.ORD:
S.ORD <dst> <str> <pos>
S.ORD asks:
What character is stored at this position, as a number?
For example:
S.ORD #code "ABC" 0
-- > #code = "65"
S.ORD #code "ABC" 1
-- > #code = "66"
The position is an ordinary string position. Negative positions count from the end:
S.ORD #code "ABC" -1
-- > #code = "67"
This also works with the special characters from the previous lesson:
S.ORD #code "\n" 0
-- > #code = "10"
The escape sequence \n produced one character, and that character has the numeric value 10.
Going the other way
S.CHAR performs the opposite conversion:
S.CHAR <dst> <code>
It takes a numeric character code and produces the corresponding character.
S.CHAR #character 65
-- > #character = "A"
S.CHAR #character 66
-- > #character = "B"
Together, S.ORD and S.CHAR let us move between characters and their numeric identities.
Unicode code points
Those numeric identities are more specifically called Unicode code points.
Unicode assigns a code point to each character.
Unicode code points are conventionally written in hexadecimal using U+ followed by the code:
| Character | Unicode code point |
|---|---|
A |
U+0041 |
Β© |
U+00A9 |
Γ© |
U+00E9 |
β |
U+2600 |
δΈ |
U+4E2D |
π |
U+1F680 |
This is where the hexadecimal notation from NCL 207 becomes useful again.
The character A has decimal code point 65.
In hexadecimal:
65ββ = 41ββ
so Unicode writes its code point as:
U+0041
S.ORD returns the code point as an ordinary NCL integer:
S.ORD #code "A" 0
-- > #code = "65"
S.ORD #code "π" 0
-- > #code = "128640"
The hexadecimal forms U+0041 and U+1F680 are conventional ways of writing those same numeric values.
You are not expected to memorize Unicode code points.
Looking up characters
The Unicode Consortium publishes code charts containing the characters and code points defined by Unicode.
When you need a particular code point, looking it up is considerably easier than memorizing thousands of hexadecimal numbers.
Code points are not bytes
For ASCII characters, code points and encoded bytes can look suspiciously similar.
Consider A:
| Property | Value |
|---|---|
| Character | A |
| Unicode code point | U+0041 |
| UTF-8 bytes | 41 |
Its Unicode code point is hexadecimal 41, and its UTF-8 representation is also the single byte 41.
That convenient correspondence does not continue for every character.
Consider the copyright symbol:
| Property | Value |
|---|---|
| Character | Β© |
| Unicode code point | U+00A9 |
| UTF-8 bytes | C2 A9 |
Β© is one Unicode code point, but UTF-8 uses two bytes to represent it.
For another example:
| Property | Value |
|---|---|
| Character | β |
| Unicode code point | U+2600 |
| UTF-8 bytes | E2 98 80 |
Again, one character and one code point, but several bytes.
The important distinction is:
Unicode identifies characters. UTF-8 encodes them.
An encoding defines how character data is represented as bytes.
UTF-8 can represent a Unicode code point using between one and four bytes.
| Character | Unicode code point | UTF-8 bytes |
|---|---|---|
A |
U+0041 | 41 |
Β© |
U+00A9 | C2 A9 |
β |
U+2600 | E2 98 80 |
π |
U+1F680 | F0 9F 9A 80 |
UTF-8, UTF-16, and UTF-32
UTF-8 is not the only encoding for Unicode.
You may also encounter UTF-16 and UTF-32. All three represent the same Unicode code points; they differ in how those code points are encoded.
| Encoding | Unit size | Units per code point | Trade-off |
|---|---|---|---|
| UTF-8 | 8 bits | 1β4 | Compact for ASCII-heavy text, but variable-width. |
| UTF-16 | 16 bits | 1β2 | Many code points fit in one unit, while others require two. |
| UTF-32 | 32 bits | 1 | Fixed-width, but uses four bytes for every code point. |
The advantages and drawbacks come from the same properties.
UTF-8 uses small units, which makes a lot of text compact but gives characters variable encoded lengths. UTF-32 gives every code point the same width, but even A occupies four bytes.
UTF-16 sits between them. Many code points use one 16-bit unit, while others use two. A two-unit UTF-16 representation is also called a surrogate pair.
The character does not change between encodings. π is still U+1F680 whether it is represented using UTF-8, UTF-16, or UTF-32.
Hexadecimal byte escapes
In NCL source, \x specifies a byte using two hexadecimal digits.
For an ASCII character, one byte is enough:
D.TXT "\x41"
D.BLT
displays:
A
But Β© requires two UTF-8 bytes:
D.TXT "\xC2\xA9"
D.BLT
and β requires three:
D.TXT "\xE2\x98\x80"
D.BLT
This works, but it requires us to know the UTF-8 representation of the character we want.
It is also worth keeping three similar-looking hexadecimal notations separate:
| Notation | Meaning |
|---|---|
0xA9 |
An NCL hexadecimal integer literal |
"\xA9" |
A hexadecimal byte escape inside a string |
| U+00A9 | A Unicode code point |
They all use hexadecimal because hexadecimal is convenient for representing binary data. They do not describe the same thing.
Unicode escapes
Usually, if we want a particular character, we would rather identify the character than manually write its encoded bytes.
NCL provides Unicode escapes for this.
The \u form specifies a Unicode code point using four hexadecimal digits:
\uXXXX
For example:
D.TXT "\u00A9"
D.BLT
displays:
Β©
Similarly:
D.TXT "\u2600"
D.BLT
displays:
β
This describes the character directly by its Unicode code point. We do not need to know that U+2600 becomes the three UTF-8 bytes E2 98 80.
Larger code points
Four hexadecimal digits can represent code points up to U+FFFF.
Unicode also contains larger code points.
For example:
π = U+1F680
For these, NCL provides the larger \U form:
\UXXXXXXXX
The rocket can therefore be written as:
D.TXT "\U0001F680"
D.BLT
The leading zeroes fill the eight hexadecimal digits required by the escape.
You can also just type the character
An escape is not required when the character can conveniently be written directly in the source.
These all produce the same resulting character:
D.TXT "β"
D.TXT "\u2600"
D.TXT "\xE2\x98\x80"
They describe it at different levels:
| NCL source | What it specifies |
|---|---|
"β" |
The character itself |
"\u2600" |
Unicode code point U+2600 |
"\xE2\x98\x80" |
UTF-8 bytes E2 98 80 |
For ordinary text, writing or pasting the character directly is usually easiest.
Unicode escapes are useful when a character is difficult to type, when its exact code point matters, or when writing the code point explicitly makes the source easier to understand.
Hexadecimal byte escapes are useful when the encoded bytes themselves matter.
Characters are still characters
Encoding does not normally change how NCL works with strings.
Consider:
S.MOVE #text "AΒ©βδΈπ"
S.LEN #length #text
The string contains five characters, so:
#length = "5"
NCL sees five string positions:
| Index | Character | Code point | UTF-8 bytes |
|---|---|---|---|
| 0 | A |
U+0041 | 41 |
| 1 | Β© |
U+00A9 | C2 A9 |
| 2 | β |
U+2600 | E2 98 80 |
| 3 | δΈ |
U+4E2D | E4 B8 AD |
| 4 | π |
U+1F680 | F0 9F 9A 80 |
The encoded byte lengths vary, but the string positions do not.
The string operations we learned earlier continue to work with complete characters:
S.SUB #character #text 4 4
-- > #character = "π"
The same applies to operations such as joining, inserting, replacing, and searching strings.
If a character has a valid Unicode code point, NCL can generally manipulate it like any other character.
Some operations require additional knowledge about the character. S.UPR and S.LWR, for example, need to know the case relationships between characters and do not necessarily have mappings for every character in Unicode.
Most string operations do not need to know what a character means. They only need to treat it as a character.
NCL strings use UTF-16
NCL strings are represented internally using UTF-16.
A character therefore occupies two bytes internally, or four bytes when UTF-16 requires two units to represent it.
Most NCL string operations work with complete Unicode characters rather than individual UTF-16 units:
S.MOVE #text "AπB"
S.LEN #length #text
gives a length of 3, even though π occupies four bytes internally.
This distinction is normally invisible. It becomes relevant mainly when working with byte escapes such as \x, converting between characters and code points with S.ORD or S.CHAR, or considering how much memory or storage a string occupies.
Otherwise, it is usually simplest to think of an NCL string as a sequence of characters.
Inspecting Unicode characters
S.ORD operates on complete Unicode code points.
For example:
S.MOVE #text "π"
S.LEN #length #text
S.ORD #code #text 0
After these instructions:
| Property | Value |
|---|---|
| Character | π |
| String length | 1 |
| String position | 0 |
| Unicode code point | U+1F680 |
| Code point in decimal | 128640 |
| UTF-8 encoding | F0 9F 9A 80 |
| UTF-8 byte count | 4 |
The character is still one item in the string.
We can also walk through a mixed string:
#text "AΒ©βπ"
MOVE #index 0
:next
S.ORD #code #text #index
D.TXT #index
D.TXT ": "
D.TXT #code
D.TXT "\r\n"
INC #index
BLT :next #index 4
D.BLT
This produces:
0: 65
1: 169
2: 9728
3: 128640
Each S.ORD reads one complete Unicode code point.
S.CHAR performs the reverse conversion, including for code points larger than U+FFFF:
S.CHAR #character 128640
-- > #character = "π"
NCL does not expose the individual UTF-16 units used internally to represent that character.
Characters and glyphs
There is one more distinction worth making.
A character is part of the text.
A glyph is a shape used to draw that character.
NCL can store and manipulate a Unicode character without requiring the Display to know how to draw it.
For example:
S.MOVE #symbol "\U0001F680"
stores the Unicode character π in #symbol.
Whether:
D.TXT #symbol
D.BLT
can draw the expected rocket depends on whether the attached Display has a glyph for U+1F680.
The Display has an extensive but finite collection of glyphs. A character outside that collection is still present in the string; the Display simply cannot draw the expected shape for it.
Other peripherals and interfaces may support different subsets of Unicode. A Printer, Modem, storage format, or external service may be able to represent characters that the Display cannot, or may reject characters that the Display handles perfectly well.
There is therefore no single yes-or-no answer to whether the entire computer can "display," "print," "send," or "store" every Unicode character.
NCL itself can generally manipulate any valid Unicode character. The limitations appear when a particular operation or interface needs additional knowledge or a particular representation of that character.
Try it
Start with a mixed string:
#text "AΒ©βδΈπ"
S.LEN #length #text
D.TXT "Length: "
D.TXT #length
D.TXT "\r\n"
S.SUB #character #text 1 1
D.TXT "Index 1: "
D.TXT #character
D.TXT "\r\n"
S.SUB #character #text 4 4
D.TXT "Index 4: "
D.TXT #character
D.TXT "\r\n"
S.ORD #code #text 4
D.TXT "Index 4 code: "
D.TXT #code
D.BLT
Before running it, predict the string length and the character extracted at each position.
Then use S.CHAR to reconstruct some characters from their decimal code points:
S.CHAR #character 65
S.CHAR #symbol 9728
Use S.ORD to verify the results.
Finally, compare three ways of creating the same character:
D.TXT "β"
D.TXT "\r\n"
D.TXT "\u2600"
D.TXT "\r\n"
D.TXT "\xE2\x98\x80"
D.BLT
All three lines contain the same resulting character.
The first writes the character directly, the second identifies its Unicode code point, and the third specifies its UTF-8 representation as bytes.
We began with a simple question: how does a computer identify one character from another?
Unicode gives characters numeric code points. Encodings such as UTF-8 and UTF-16 define how those code points are represented as data. NCL normally hides those encoding details and lets string operations work with complete characters.
Rendering adds another layer: having a character in a string does not guarantee that a particular device has a glyph or representation for it.
In the next lesson, NCL 210: Styling the Display, we'll use another kind of escape sequence to control how those characters are displayed.