如何在Lua中将字符编码转换为字符串字符?

3
如何在Lua中将字符编码转换为字符串字符?
例如:
d = 48
-- this is what I want
str_d = "0"
2个回答

5
你正在寻找 string.char
string.char (···)

Receives zero or more integers. Returns a string with length equal to the number of arguments, in which each character has the internal numerical code equal to its corresponding argument.

Note that numerical codes are not necessarily portable across platforms.

针对您的例子:

local d = 48
local str_d = string.char(d) -- str_d == "0"

2

对于ASCII字符,您可以使用string.char

对于UTF-8字符串,您可以使用utf8.char(在Lua 5.3中引入)以获取其代码点的字符。

print(utf8.char(48))    -- 0
print(utf8.char(29790)) -- 瑞

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接