utf8_to_cp1252 = (
function(cp1252_description)
local unicode_to_1252 = {}
for code, unicode in cp1252_description:gmatch'\n0x(%x%x)%s+0x(%x+)' do
unicode_to_1252[tonumber(unicode, 16)] = tonumber(code, 16)
end
local undefined = ('?'):byte()
return
function (utf8str)
local pos, result = 1, {}
while pos <= #utf8str do
local code, size = utf8str:byte(pos, pos), 1
if code >= 0xC0 and code < 0xFE then
local mask = 64
code = code - 128
repeat
local next_byte = utf8str:byte(pos+size, pos+size) or 0
if next_byte >= 0x80 and next_byte < 0xC0 then
code, size = (code - mask - 2) * 64 + next_byte, size+1
else
code, size = utf8str:byte(pos, pos), 1
end
mask = mask * 32
until code < mask
end
pos = pos + size
table.insert(result,
string.char(unicode_to_1252[code] or undefined))
end
return table.concat(result)
end
end
)[[
download
http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
and insert the whole text here:
#
# Name: cp1252 to Unicode table
# Unicode version: 2.0
# Table version: 2.01
..................................
0xFD 0x00FD #LATIN SMALL LETTER Y WITH ACUTE
0xFE 0x00FE #LATIN SMALL LETTER THORN
0xFF 0x00FF #LATIN SMALL LETTER Y WITH DIAERESIS
]]
用法:
cp1252_filename = utf8_to_cp1252(your_utf8_filename)
现在您可以使用标准 Lua 库中cp1252_filename
的来调用io.open()
、os.rename()
和os.execute()
其他函数。