2011-11-02 8 views
6

ルアにどのようにしてユニコードシンボルを書くことができますか?たとえば、私は私がルアでユニコードシンボルを書くには

string.char(9658); 

を書くとき、私はエラーを得た9658
と記号を記述する必要があります。だから、どうやってそのようなシンボルを書くことができるのですか?

+1

を – MattJ

答えて

12

Luaは内部の文字列を検索しません。 Unicode文字のUTF-8エンコーディングが可能

:だから、あなただけ

のLua(2015年追加)UTF-8エスケープシーケンス5.3導入支援を

mychar = "►" 

を書くことができますエスケープシーケンス\ u {XXX}を持つリテラル文字列に挿入されます(必須の囲み括弧に注意してください)。XXXは、文字コードポイントを表す1つ以上の16進数字のシーケンスです。

utf8.char(9658)も使用できます。

+2

これは、ファイル自体がUTF-8でエンコードされている場合にのみ機能することに注意してください。もちろん、ASCIIまたはUTF-8でない限り、インタプリタでLuaスクリプトを実行することはできません。 –

2

多分これはあなたを助けることができる:

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

I機能が彼が望んでいるのとは正反対だと確信している。彼はUTF-8でエンコードしたいUnicodeコードポイントを持っています。 –

+0

反対も遠くまで行くことができます! :) –

2

Unicode文字列コンテンツの幅広いサポートを得るには、Seleneデータベースライブラリの一部として開発されたslnunicodeがあります。これは標準のstringライブラリの機能をサポートするモジュールを提供しますが、Unicode文字とUTF-8エンコーディングを使用します。ここで

3

は、Unicodeコードポイントを取り、対応する文字のUTF-8文字列を生成Luaのためのエンコーダです。それはあなたが結果の文字列を望むものエンコーディングを知るために役立つだろう

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.