08.05.2017, 06:58
Try this, it only handles 1 and 2 byte utf8 characters, but this should be enough for your case:
Code:
function utf8cps(str)
local codepoints = {}
local len = #str
local i = 1
while i <= len do
local c = str:byte(i, i)
if c > 0 and c <= 127 then
codepoints[ #codepoints + 1 ] = c
i = i + 1
elseif c >= 194 and c <= 223 then
local c2 = str:byte(i + 1, i + 1) or 0
local cp = bit.bor(bit.lshift(c - 0xC0, 6), c2 - 0x80)
codepoints[ #codepoints + 1 ] = cp
i = i + 2
elseif c >= 224 and c <= 239 then
i = i + 3
elseif c >= 240 and c <= 244 then
i = i + 4
end
end
return codepoints
end
function toutf16hex(str)
local cps = utf8cps(str)
local res = {}
for _, cp in ipairs(cps) do
res[ #res + 1 ] = string.format('%04x', cp)
end
return table.concat(res)
end
encoded =toutf16hex('This is a test')
alert(encoded)