使用 Lua 将字符串转换为表情符号

我正在开发一种编码算法,可以将字母转换为表情符号,但是我的算法在某些字符上返回 nil,为什么?

local function enc(MSG)
    local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X","Y", "Z", " ", ".", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_", "*", "'", "^", "~", "!"}
    local resourcesT = {"", "", "", "", "", "", "⛹", "", "㊗️", "", "", "", "", "", "", "☁️", "", "", "", "", "", "", "", "", "", "⛄️","", "↪️", "", "", "❗️", "", "", "", "", "️", "", "", "", "⚫️", "", "⭕️", "", "☣", "", "⏯", "", "", "", "", "⌛️", "⌨", "", "", "⏫", "", "", "", "", "", "", "", "", "", "", "#️⃣", "", "", "✖️", ""}
    local finalstring = ""
    local count = 0
    MSG:gsub(".", function(c)
    for i,v in pairs(resources) do
              count = count + 1
               if v == c then
                 finalstring = finalstring .. tostring(resourcesT[count])
                 count = 0
             end
         end
    end)
    return finalstring
end

local Enc = enc("Hello World")
local Thread = io.open("l.lua", "w")
Thread:write(Enc)
点赞
用户6367889
用户6367889

你不需要跟踪 count 变量,只需要使用现有的 i 变量,并且它将能够正常工作:

``` local function enc(MSG) local resources = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X","Y", "Z", " ", ".", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_", "*", "'", "^", "~", "!"} local resourcesT = {"", "", "", "", "", "", "⛹", "", "㊗️", "", "", "", "", "", "", "☁️", "", "", "", "", "", "", "", "", "", "⛄️","", "↪️", "", "", "❗️", "", "", "", "", "️", "", "", "", "⚫️", "", "⭕️", "", "☣", "", "⏯", "", "", "", "", "⌛️", "⌨", "", "", "⏫", "", "", "", "", "", "", "", "", "", "", "#️⃣", "", "", "âœ

2021-07-21 00:54:04