lua 字符转换 utf8

作者:雨辰 发布于:2017-8-22 20:05 Tuesday 分类:Lua

function utf8(decimal) -- convert unicode code point to utf-8 encoded character string
	if decimal<128 then return char(decimal) end
	local charbytes = {}
	for bytes,vals in ipairs(utf8markers) do
		if decimal<=vals[1] then
			for b=bytes+1,2,-1 do
				local mod = decimal%64
				decimal = (decimal-mod)/64
				charbytes[b] = char(128+mod)
			end
			charbytes[1] = char(vals[2]+decimal)
			return concat(charbytes)
		end
	end
end

标签: lua

发表评论:

雨辰 joyimp|@2011-2017 京ICP备16030765号