lua的utf8互转gbk编码方法, 基于linux下实现,使用lua-iconv的.
安装:
luarocks install lua-iconv
安装过程感觉有点慢,那个lua-iconv网址有时获取不到,不行的时候就再尝试下吧.
然后以下就是转换代码:
local iconv = require("iconv")
http=require("socket.http")
function createIconv(from,to,text)
local cd = iconv.new(to .. "//TRANSLIT", from)
local ostr, err = cd:iconv(text)
if err == iconv.ERROR_INCOMPLETE then
return "ERROR: Incomplete input."
elseif err == iconv.ERROR_INVALID then
return "ERROR: Invalid input."
elseif err == iconv.ERROR_NO_MEMORY then
return "ERROR: Failed to allocate memory."
elseif err == iconv.ERROR_UNKNOWN then
return "ERROR: There was an unknown error."
end
return ostr
end
result=http.request("http://www.lpfrx.com")
print(createIconv("utf-8","gbk",result))