调用实例
require("ZZBase64")
local files
local file = io.open("E:\\2342.jpg","rb") if file then
print "发现文件"
files = file:read("*a")
file:close()
else
print "没有找到文件"
end --图片转成base64文本
print(ZZBase64.encode(files))
帮助类
ZZBase64 = {}
local string = string ZZBase64.__code = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '', '', '', '', '', '', '', '', '', '', '+', '/',
};
ZZBase64.__decode = {}
for k,v in pairs(ZZBase64.__code) do
ZZBase64.__decode[string.byte(v,)] = k -
end function ZZBase64.encode(text)
local len = string.len(text)
local left = len %
len = len - left
local res = {}
local index =
for i = , len, do
local a = string.byte(text, i )
local b = string.byte(text, i + )
local c = string.byte(text, i + )
-- num = a<<16 + b<<8 + c
local num = a * + b * + c
for j = , do
--tmp = num >> ((4 -j) * 6)
local tmp = math.floor(num / ( ^ ((-j) * )))
--curPos = tmp&0x3f
local curPos = tmp % +
res[index] = ZZBase64.__code[curPos]
index = index +
end
end if left == then
ZZBase64.__left1(res, index, text, len)
elseif left == then
ZZBase64.__left2(res, index, text, len)
end
return table.concat(res)
end function ZZBase64.__left2(res, index, text, len)
local num1 = string.byte(text, len + )
num1 = num1 * --lshift 10
local num2 = string.byte(text, len + )
num2 = num2 * --lshift 2
local num = num1 + num2 local tmp1 = math.floor(num / ) --rShift 12
local curPos = tmp1 % +
res[index] = ZZBase64.__code[curPos] local tmp2 = math.floor(num / )
curPos = tmp2 % +
res[index + ] = ZZBase64.__code[curPos] curPos = num % +
res[index + ] = ZZBase64.__code[curPos] res[index + ] = "="
end function ZZBase64.__left1(res, index,text, len)
local num = string.byte(text, len + )
num = num * tmp = math.floor(num / )
local curPos = tmp % +
res[index ] = ZZBase64.__code[curPos] curPos = num % +
res[index + ] = ZZBase64.__code[curPos] res[index + ] = "="
res[index + ] = "="
end function ZZBase64.decode(text)
local len = string.len(text)
local left =
if string.sub(text, len - ) == "==" then
left =
len = len -
elseif string.sub(text, len) == "=" then
left =
len = len -
end local res = {}
local index =
local decode = ZZBase64.__decode
for i =, len, do
local a = decode[string.byte(text,i )]
local b = decode[string.byte(text,i + )]
local c = decode[string.byte(text,i + )]
local d = decode[string.byte(text,i + )] --num = a<<18 + b<<12 + c<<6 + d
local num = a * + b * + c * + d local e = string.char(num % )
num = math.floor(num / )
local f = string.char(num % )
num = math.floor(num / )
res[index ] = string.char(num % )
res[index + ] = f
res[index + ] = e
index = index +
end if left == then
ZZBase64.__decodeLeft1(res, index, text, len)
elseif left == then
ZZBase64.__decodeLeft2(res, index, text, len)
end
return table.concat(res)
end function ZZBase64.__decodeLeft1(res, index, text, len)
local decode = ZZBase64.__decode
local a = decode[string.byte(text, len + )]
local b = decode[string.byte(text, len + )]
local c = decode[string.byte(text, len + )]
local num = a * + b * + c local num1 = math.floor(num / ) %
local num2 = math.floor(num / ) %
res[index] = string.char(num1)
res[index + ] = string.char(num2)
end function ZZBase64.__decodeLeft2(res, index, text, len)
local decode = ZZBase64.__decode
local a = decode[string.byte(text, len + )]
local b = decode[string.byte(text, len + )]
local num = a * + b
num = math.floor(num / )
res[index] = string.char(num)
end