lua 获取Unicode值对应字符

-- value: Unicode值
-- str  : Unicode值对应字符
GameUtils.UnicodeToUTF_8Str = function(value)
    local str
    -- 先把Unicode值表示二进制转成Utf_8格式, 在string.char返回字符
    if value < 128 then -- (Utf_8格式 = [0xxxxxxx])(128 = 1 0000000)
        str = string.char(value)
    elseif value < 2048 then -- (Utf_8格式 = [110xxxxx]-[10xxxxxx]) (2048 = 1 00000 000000)
        local byte1 = 128 + value % 64
        local byte2 = 192 + math.floor(value / 64)
        str = string.char(byte2, byte1)
    elseif value < 65536 then -- (Utf_8格式 = [1110xxxx]-[10xxxxxx]-[10xxxxxx]) (65536 = 1 0000 000000 000000)
        local byte1 = 128 + value % 64
        local byte2 = 128 + (math.floor(value / 64) % 32)
        local byte3 = 224 + (math.floor(value / 4096) % 16)
        str = string.char(byte3, byte2, byte1)
    end
    return str
end

猜你喜欢

转载自blog.csdn.net/qq_23602395/article/details/105271783
LUA