1

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?

4

3 回答 3

3

在 Lua 5.3 中,您可以使用utf8.codepoint获取 UTF-8 字符串的代码点。

print(utf8.codepoint("瑞"))
--29790
于 2015-01-14T11:18:54.133 回答
2

对于 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,可能无法以您的本地字体正确显示)

于 2012-11-27T06:18:19.583 回答
1

有几个答案可能会给您想要的东西(如果您将自己限制为 UTF8):

于 2012-11-27T06:36:16.267 回答