lua试图调用方法';len';(零值)
在我的lua项目中,我获得了以下功能:lua试图调用方法';len';(零值),lua,Lua,在我的lua项目中,我获得了以下功能: function module.Cp1251ToUtf8(s) if s == nil then return nil end local r, b = '' for i = 1, s and s:len() or 0 do --the problem occurs here b = s:byte(i) if b < 128 then r = r..string.char(b) else
function module.Cp1251ToUtf8(s)
if s == nil then return nil end
local r, b = ''
for i = 1, s and s:len() or 0 do --the problem occurs here
b = s:byte(i)
if b < 128 then
r = r..string.char(b)
else
if b > 239 then
r = r..'\209'..string.char(b - 112)
elseif b > 191 then
r = r..'\208'..string.char(b - 48)
elseif cp1251_decode[b] then
r = r..cp1251_decode[b]
else
r = r..'_'
end
end
end
return r
end
功能模块.cp1251touth8(s)
如果s==nil,则返回nil end
局部r,b=''
对于i=1,s和s:len()或0 do,问题出现在这里
b=s:字节(i)
如果b<128,则
r=r..string.char(b)
其他的
如果b>239,则
r=r..'\209'..string.char(b-112)
如果b>191那么
r=r..'\208'..string.char(b-48)
elseif cp1251_解码[b]然后
r=r..cp1251_解码[b]
其他的
r=r.“'
结束
结束
结束
返回r
结束
据我所知,这个函数获取一个字符串并转换其编码。有时它工作正常,但有时会出现以下错误:尝试调用方法'len'(一个nil值)
。有什么想法吗?它是什么?如何修复
我试图删除s:len()
或插入类似if s!=然后是零…
,但它也不起作用
if s == nil then return nil end
以上拒绝零值。但还有其他非字符串,因此请严格检查:
if type(s) ~= 'string' return nil end