I've done it before, but I'm not certain how and I have since lost the source files.
How do I get the code point of a character in Lua? Or, at least, a unique value for a character?
在 Lua 5.3 中,您可以使用utf8.codepoint获取 UTF-8 字符串的代码点。
print(utf8.codepoint("瑞"))
--29790
对于 ASCII 字符串,这很容易:
local char_code = string.byte("A",1);
-- char_code now contains 65
对于 UTF-8(假设这是您表示数据的方式),它变得很棘手。要么使用slnunicode 之类的第 3 方库,要么您必须编写自己的函数来解析 UTF-8 字节。
你的 Lua 安装可能已经包含ValidateUnicodeString扩展,它允许它工作:
local char_code = string.utf8code("ٱ");
-- char_code now contains 1649
(该示例包含阿拉伯语 Alef Wasla,可能无法以您的本地字体正确显示)
有几个答案可能会给您想要的东西(如果您将自己限制为 UTF8):