This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Multiple event-scripts in 1 script
#16
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)
Reply


Messages In This Thread
Multiple event-scripts in 1 script - by Mr.D - 24.04.2017, 17:33
RE: Multiple event-scripts in 1 script - by admin - 08.05.2017, 06:58

Forum Jump: