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("https://www.lpfrx.com") print(createIconv("utf-8","gbk",result))
反正就是lua库的问题,windows下luarocks安装不了lua-iconv, 这个要再找找怎么样解决,反正有时间先.
lua-iconv是依靠c/c++实现的吗?node里面有一个iconv-lite的库,是通过javascript字码对照实现转码的。