Character encodings#
In Lua and with software in general, there is no such thing as plain text: everything is encoded as binary, generally organized in bytes (groups of 8 bits), and text is yet another construct above bytes by using what is known as character encodings.
Character encodings are the art of representing text as a sequence of characters over bytes. This is something that also applies in Lua, as Lua strings are nothing more than sequences of bytes with no validation or transformation; interpreting them as text is up to the programs and native APIs.
For the sake of simplicity, we’ll consider that encodings are a way to encode a sequence of codepoints, where each codepoint is a numerical value representing a character within bounds, over a sequence of bytes, and that for a sequence of codepoints, there is exactly one way to represent it over a sequence of bytes. Note that this doesn’t mean a text has only one representation; see Unicode normalization on that subject.
Real-world uses Unicode encodings, mostly, from the point of view of ComputerCraft:
The host system running Java might be using UTF-16 (Windows API implement this, as Microsoft was an early adopter of Unicode), UTF-8 (for most common systems) or earlier encodings such as ASCII, an ISO-8859 variant, Windows-1252, Shift JIS, etc (generally on systems dating from before 2000), or even vendor-specific encodings.
Java uses UCS-2 as its native character and string type, which is limited to the BMP (sorry emojis, you won’t fit in).
Most of the web accessible through HTTP uses UTF-8, but lots of earlier websites still use ISO-8859 variants.
In this document, I’ll describe common character encodings encountered while programming for or contributing to thox.
ComputerCraft encoding#
Monitors and terminals in ComputerCraft use a custom 8-bit encoding derived from ISO 8859-1, with the 00-1F range backported from Code page 437 (with some exceptions) and the 80-9F ranges replaced with custom graphical characters; the table above corresponds to the 8x11 glyphs defined in the mod.
This comes from the fact that Cobalt, the LuaJ fork specific to ComputerCraft, i.e. the Lua interpreter, decodes and encodes a Lua string by mapping byte values to Unicode codepoints; see LuaString.decode.
This encoding is used in every string passed on to the Lua code, and expected from every string passed on by the Lua code to the native APIs. When using native ComputerCraft or emulator (e.g. CCEmuX), there is no proper mapping operation when pasting to a terminal: for example, the character “♫” (U+266B) is not properly converted into the character 0x0E (15), instead only being converted to 0x3F (char. 63, “?”).
The code page layout is the following:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
NUL00000
|
☺
263A1
|
☻
263B2
|
♥
26653
|
♦
26664
|
♣
26635
|
♠
26606
|
•
20227
|
◘
25D88
|
♂
264211
|
♀
264012
|
♪
266A14
|
♫
266B15
|
|||
|
►
25BA16
|
◄
25C417
|
↕
219518
|
‼
203C19
|
¶
00B620
|
§
00A721
|
▬
25AC22
|
↨
21A823
|
↑
219124
|
↓
219325
|
→
219226
|
←
219027
|
∟
221F28
|
↔
219429
|
▲
25B230
|
▼
25BC31
|
|
SP002032
|
!
002133
|
"
002234
|
#
002335
|
$
002436
|
%
002537
|
&
002638
|
'
002739
|
(
002840
|
)
002941
|
*
002A42
|
+
002B43
|
,
002C44
|
-
002D45
|
.
002E46
|
/
002F47
|
|
0
003048
|
1
003149
|
2
003250
|
3
003351
|
4
003452
|
5
003553
|
6
003654
|
7
003755
|
8
003856
|
9
003957
|
:
003A58
|
;
003B59
|
<
003C60
|
=
003D61
|
>
003E62
|
?
003F63
|
|
@
004064
|
A
004165
|
B
004266
|
C
004367
|
D
004468
|
E
004569
|
F
004670
|
G
004771
|
H
004872
|
I
004973
|
J
004A74
|
K
004B75
|
L
004C76
|
M
004D77
|
N
004E78
|
O
004F79
|
|
P
005080
|
Q
005181
|
R
005282
|
S
005383
|
T
005484
|
U
005585
|
V
005686
|
W
005787
|
X
005888
|
Y
005989
|
Z
005A90
|
[
005B91
|
\
005C92
|
]
005D93
|
^
005E94
|
_
005F95
|
|
`
006096
|
a
006197
|
b
006298
|
c
006399
|
d
0064100
|
e
0065101
|
f
0066102
|
g
0067103
|
h
0068104
|
i
0069105
|
j
006A106
|
k
006B107
|
l
006C108
|
m
006D109
|
n
006E110
|
o
006F111
|
|
p
0070112
|
q
0071113
|
r
0072114
|
s
0073115
|
t
0074116
|
u
0075117
|
v
0076118
|
w
0077119
|
x
0078120
|
y
0079121
|
z
007A122
|
{
007B123
|
|
007C124
|
}
007D125
|
~
007E126
|
🮙
1FB99127
|
|
EMQ2001128
|
🬀
1FB00129
|
🬁
1FB01130
|
🬂
1FB02131
|
🬃
1FB03132
|
🬄
1FB04133
|
🬅
1FB05134
|
🬆
1FB06135
|
🬇
1FB07136
|
🬈
1FB08137
|
🬉
1FB09138
|
🬊
1FB0A139
|
🬋
1FB0B140
|
🬌
1FB0C141
|
🬍
1FB0D142
|
🬎
1FB0E143
|
|
🬏
1FB0F144
|
🬐
1FB10145
|
🬑
1FB11146
|
🬒
1FB12147
|
🬓
1FB13148
|
▌
258C149
|
🬔
1FB14150
|
🬕
1FB15151
|
🬖
1FB16152
|
🬗
1FB17153
|
🬘
1FB18154
|
🬙
1FB19155
|
🬚
1FB1A156
|
🬛
1FB1B157
|
🬜
1FB1C158
|
🬝
1FB1D159
|
|
NBSP00A0160
|
¡
00A1161
|
¢
00A2162
|
£
00A3163
|
¤
00A4164
|
¥
00A5165
|
¦
00A6166
|
§
00A7167
|
¨
00A8168
|
©
00A9169
|
ª
00AA170
|
«
00AB171
|
¬
00AC172
|
SHY00AD173
|
®
00AE174
|
¯
00AF175
|
|
°
00B0176
|
±
00B1177
|
²
00B2178
|
³
00B3179
|
´
00B4180
|
µ
00B5181
|
¶
00B6182
|
·
00B7183
|
¸
00B8184
|
¹
00B9185
|
º
00BA186
|
»
00BB187
|
¼
00BC188
|
½
00BD189
|
¾
00BE190
|
¿
00BF191
|
|
À
00C0192
|
Á
00C1193
|
Â
00C2194
|
Ã
00C3195
|
Ä
00C4196
|
Å
00C5197
|
Æ
00C6198
|
Ç
00C7199
|
È
00C8200
|
É
00C9201
|
Ê
00CA202
|
Ë
00CB203
|
Ì
00CC204
|
Í
00CD205
|
Î
00CE206
|
Ï
00CF207
|
|
Ð
00D0208
|
Ñ
00D1209
|
Ò
00D2210
|
Ó
00D3211
|
Ô
00D4212
|
Õ
00D5213
|
Ö
00D6214
|
×
00D7215
|
Ø
00D8216
|
Ù
00D9217
|
Ú
00DA218
|
Û
00DB219
|
Ü
00DC220
|
Ý
00DD221
|
Þ
00DE222
|
ß
00DF223
|
|
à
00E0224
|
á
00E1225
|
â
00E2226
|
ã
00E3227
|
ä
00E4228
|
å
00E5229
|
æ
00E6230
|
ç
00E7231
|
è
00E8232
|
é
00E9233
|
ê
00EA234
|
ë
00EB235
|
ì
00EC236
|
í
00ED237
|
î
00EE238
|
ï
00EF239
|
|
ð
00F0240
|
ñ
00F1241
|
ò
00F2242
|
ó
00F3243
|
ô
00F4244
|
õ
00F5245
|
ö
00F6246
|
÷
00F7247
|
ø
00F8248
|
ù
00F9249
|
ú
00FA250
|
û
00FB251
|
ü
00FC252
|
ý
00FD253
|
þ
00FE254
|
ÿ
00FF255
|
OpenComputers encoding#
OpenComputers uses UTF-8 as defined in RFC2279 as its main encoding.
However, only a subset of characters can be displayed, the other characters
being displayed as “?” (U+003F). You can find the list of characters
in encodings-oc-characters.txt.
See LuaString.valueOf and LuaString.tojstring (OC-LuaJ version), StaticFontRenderer.drawChar and chars.txt for reference.