-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnicode
65 lines (60 loc) · 1.49 KB
/
Unicode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function case1UnicodeToUtf8(unicodeValue)
local u = unicodeValue
local byte0 = (u % 0x80)
local utf8 = string.char(byte0)
return utf8
end
function case2UnicodeToUtf8(unicodeValue)
local u = unicodeValue
local byte1 = (0x80 + (u % 0x40) )
u = math.floor(u / 0x40)
local byte0 = (0xc0 + (u % 0x20) )
local utf8 = string.char(byte0, byte1)
return utf8
end
function case3UnicodeToUtf8(unicodeValue)
local u = unicodeValue
local byte2 = (0x80 + (u % 0x40))
u = math.floor(u / 0x40)
local byte1 = (0x80 + (u % 0x40))
u = math.floor(u / 0x40)
local byte0 = (0xe0 + (u % 0x10))
local utf8 = string.char(byte0, byte1, byte2)
return utf8
end
function case4UnicodeToUtf8(unicodeValue)
local u = unicodeValue
local byte3 = (0x80 + (u % 0x40))
u = math.floor(u / 0x40)
local byte2 = (0x80 + (u % 0x40))
u = math.floor(u / 0x40)
local byte1 = (0x80 + (u % 0x40))
u = math.floor(u / 0x40)
local byte0 = (0xf0 + (u % 0x8))
local utf8 = string.char(byte0, byte1, byte2, byte3)
return utf8
end
function unicodeToUtf8(unicodeValue)
local u = unicodeValue
if type(u) == "string" then
u = tonumber(u,16)
end
if ((0x800 <= u) and (0xffff >= u))
then
return case3UnicodeToUtf8(u)
end
if ((0x80 <= u) and (0x7fff >= u))
then
return case2UnicodeToUtf8(u)
end
if ((0x0 <= u) and (0x7f >= u))
then
return case1UnicodeToUtf8(u)
end
if( (0x10000 <= u) and (0x10ffff >= u) )
then
return case4UnicodeToUtf8(u)
end
return nil
end
return unicodeToUtf8