function hashConvert(v,ext)
local ch = 0
local val = 0
if(v) then
for i=1,#v do
ch = v:byte(i)
if( ch >= 65 and ch <= 90 ) then
ch = ch + 32
end
val = val*0.7 + ch --0.7是加权
end
end
val = val .. ''
val = val:gsub("+","")
val = val:gsub("%.","")
if(ext)then
return string.format('%s.%s',val,ext)
else
return string.format('%s',val)
end
end
--test
print(hashConvert('http://192.168.1.131:8080/sns/head.jsp?id=3&width=40&height=40&type=jpeg','gif'))
print(hashConvert('http://192.168.1.131:8080/sns/head.jsp?id=5&width=40&height=40&type=jpeg','gif'))
--output
33444727508723.gif
33444733948534.gif
使用string 2 hashcode 在有些场合很方便, 比如页面图片缓存等。