Advertisement
jille_Jr

CC: BOM test

Nov 17th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.61 KB | None | 0 0
  1. function Utf8to32(utf8str)
  2.     assert(type(utf8str) == "string")
  3.     local res, seq, val = {}, 0, nil
  4.     for i = 1, #utf8str do
  5.         local c = string.byte(utf8str, i)
  6.         if seq == 0 then
  7.             table.insert(res, val)
  8.             seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
  9.                   c < 0xF8 and 4 or c < 0xFC and 5 or c < 0xFE and 6 or
  10.                   error("invalid UTF-8 character sequence")
  11.             val = bit.band(c, 2^(8-seq) - 1)
  12.         else
  13.             val = bit.bor(bit.lshift(val, 6), bit.band(c, 0x3F))
  14.         end
  15.         seq = seq - 1
  16.     end
  17.     table.insert(res, val)
  18.     table.insert(res, 0)
  19.     return res
  20. end
  21.  
  22. print(textutils.serialize(Utf8to32(...)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement