How to write unicode character in lua

How to write unicode character in lua. For example, I have to write a character with 9658
when I write

string.char( 9658 ); 

I got an error. So, how can you write such a character.

+7
source share
4 answers

Lua does not look inside the lines. So you can just write

 mychar = "►" 

(added in 2015)

Lua 5.3 introduced support for UTF-8 escape sequences:

The Unicode character UTF-8 encoding can be inserted into a literal string with the escape sequence \ u {XXX} (note the required enclosed brackets), where XXX is a sequence of one or more hexadecimal digits representing the character code point.

You can also use utf8.char(9658) .

+12
source

Here is an encoder for Lua that takes a Unicode code point and creates a UTF-8 string for the corresponding character:

 do local bytemarkers = { {0x7FF,192}, {0xFFFF,224}, {0x1FFFFF,240} } function utf8(decimal) if decimal<128 then return string.char(decimal) end local charbytes = {} for bytes,vals in ipairs(bytemarkers) do if decimal<=vals[1] then for b=bytes+1,2,-1 do local mod = decimal%64 decimal = (decimal-mod)/64 charbytes[b] = string.char(128+mod) end charbytes[1] = string.char(vals[2]+decimal) break end end return table.concat(charbytes) end end c=utf8(0x24) print(c.." is "..#c.." bytes.") --> $ is 1 bytes. c=utf8(0xA2) print(c.." is "..#c.." bytes.") --> ¢ is 2 bytes. c=utf8(0x20AC) print(c.." is "..#c.." bytes.") --> € is 3 bytes. c=utf8(0x24B62) print(c.." is "..#c.." bytes.") --> 𤭢 is 4 bytes. 
+3
source

Perhaps this may help you:

  function FromUTF8(pos) local mod = math.mod local function charat(p) local v = editor.CharAt[p]; if v < 0 then v = v + 256 end; return v end local v, c, n = 0, charat(pos), 1 if c < 128 then v = c elseif c < 192 then error("Byte values between 0x80 to 0xBF cannot start a multibyte sequence") elseif c < 224 then v = mod(c, 32); n = 2 elseif c < 240 then v = mod(c, 16); n = 3 elseif c < 248 then v = mod(c, 8); n = 4 elseif c < 252 then v = mod(c, 4); n = 5 elseif c < 254 then v = mod(c, 2); n = 6 else error("Byte values between 0xFE and OxFF cannot start a multibyte sequence") end for i = 2, n do pos = pos + 1; c = charat(pos) if c < 128 or c > 191 then error("Following bytes must have values between 0x80 and 0xBF") end v = v * 64 + mod(c, 64) end return v, pos, n end 
+2
source

To get wider support for Unicode string content, one approach is slnunicode , which was developed as part of the Selene database library. It will provide you with a module that supports most of the standard string library, but with Unicode characters and UTF-8 encoding.

+2
source

All Articles