如何在lua中编写Unicode符号.例如,
当我写作时,我必须用9658写符号
string.char( 9658 );
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误.那么如何编写这样的符号呢?
lhf*_*lhf 13
Lua不看内部字符串.所以,你可以写
mychar = "?"
Run Code Online (Sandbox Code Playgroud)
(2015年新增)
Lua 5.3引入了对UTF-8转义序列的支持:
可以将Unicode字符的UTF-8编码插入到具有转义序列\ u {XXX}的文字字符串中(请注意必需的括号),其中XXX是表示字符代码点的一个或多个十六进制数字的序列.
你也可以使用utf8.char(9658).
这是 Lua 的编码器,它采用 Unicode 代码点并为相应的字符生成 UTF-8 字符串:
do
local bytemarkers = { {0x7FF,192}, {0xFFFF,224}, {0x1FFFFF,240} }
function utf8(decimal)
if decimal<128 then return string.char(decimal) end
local charbytes = {}
for bytes,vals in ipairs(bytemarkers) do
if decimal<=vals[1] then
for b=bytes+1,2,-1 do
local mod = decimal%64
decimal = (decimal-mod)/64
charbytes[b] = string.char(128+mod)
end
charbytes[1] = string.char(vals[2]+decimal)
break
end
end
return table.concat(charbytes)
end
end
c=utf8(0x24) print(c.." is "..#c.." bytes.") --> $ is 1 bytes.
c=utf8(0xA2) print(c.." is "..#c.." bytes.") --> ¢ is 2 bytes.
c=utf8(0x20AC) print(c.." is "..#c.." bytes.") --> € is 3 bytes.
c=utf8(0x24B62) print(c.." is "..#c.." bytes.") --> is 4 bytes.
Run Code Online (Sandbox Code Playgroud)