如何在lua中编写Unicode符号。例如我必须用 9658 来写符号
当我写作时
string.char( 9658 );
我收到一个错误。那么怎么可能写出这样的符号呢。
Lua 不查看字符串内部。所以,你可以写
mychar = "►"
(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.
也许这可以帮助你:
function FromUTF8(pos)
local mod = math.mod
local function charat(p)
local v = editor.CharAt[p]; if v < 0 then v = v + 256 end; return v
end
local v, c, n = 0, charat(pos), 1
if c < 128 then v = c
elseif c < 192 then
error("Byte values between 0x80 to 0xBF cannot start a multibyte sequence")
elseif c < 224 then v = mod(c, 32); n = 2
elseif c < 240 then v = mod(c, 16); n = 3
elseif c < 248 then v = mod(c, 8); n = 4
elseif c < 252 then v = mod(c, 4); n = 5
elseif c < 254 then v = mod(c, 2); n = 6
else
error("Byte values between 0xFE and OxFF cannot start a multibyte sequence")
end
for i = 2, n do
pos = pos + 1; c = charat(pos)
if c < 128 or c > 191 then
error("Following bytes must have values between 0x80 and 0xBF")
end
v = v * 64 + mod(c, 64)
end
return v, pos, n
end
为了获得对 Unicode 字符串内容的更广泛支持,一种方法是 slnunicode,它是作为 Selene 数据库的一部分开发的。它将为您提供一个支持标准
string
库的大部分功能的模块,但支持 Unicode 字符和 UTF-8 编码。
Lua代码:
function utf8Char (decimal)
if decimal < 128 then
return string.char(decimal)
elseif decimal < 2048 then
local byte2 = string.char(128 + (decimal % 64))
local byte1 = string.char(192 + math.floor(decimal / 64))
return byte1 .. byte2
elseif decimal < 65536 then
local byte3 = string.char(128 + (decimal % 64))
decimal = math.floor(decimal / 64)
local byte2 = string.char(128 + (decimal % 64))
local byte1 = string.char(224 + math.floor(decimal / 64))
return byte1 .. byte2 .. byte3
elseif decimal < 1114112 then
local byte4 = string.char(128 + (decimal % 64))
decimal = math.floor(decimal / 64)
local byte3 = string.char(128 + (decimal % 64))
decimal = math.floor(decimal / 64)
local byte2 = string.char(128 + (decimal % 64))
local byte1 = string.char(240 + math.floor(decimal / 64))
return byte1 .. byte2 .. byte3 .. byte4
else
return nil -- Invalid Unicode code point
end
end
示例:
print ('65', utf8Char (65))
print ('255', utf8Char (255))
print ('256', utf8Char (256))
print ('512', utf8Char (512))
print ('1060', utf8Char (1060))
print ('2768', utf8Char (2768))
print ('12040', utf8Char (12040))
print ('64256', utf8Char (64256))
print ('66360', utf8Char (66360))
输出:
65 A
255 ÿ
256 Ā
512 Ȁ
1060 Ф
2768 ૐ
12040 ⼈
64256 ff
66360 𐌸