关于base64 -- 集中讨论
[code]-- encryption tablelocal base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',
[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',
[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',
[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='+',[63]='/'}
-- function encode
-- encodes input string to base64.
function base64_encode(data)
local bytes = {}
local result = ""
for spos=0,string.len(data)-1,3 do
for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
result = string.format('%s%s%s%s%s',result,base64chars[math.floor(bytes[1]/4)] or "=",
base64chars[(bytes[1] % 4) * 16 + math.floor(bytes[2] / 16)] or "=",
({[true]=base64chars[(bytes[2] % 16) * 4 + math.floor(bytes[3] / 64)] or "=",[false]="="})[(string.len(data)-spos) > 1],
({[true]=base64chars[(bytes[3] % 64)] or "=",[false]="="})[(string.len(data)-spos) > 2])
end
return result
end[/code][b]base64[/b] 算法编码还是不太懂.. MM数据都是双精度浮点运算出来的
[url=http://lua-users.org/wiki/BaseSixtyFour]http://lua-users.org/wiki/BaseSixtyFour[/url] 参考此处比较好
S fr 来讲解下base64 我已经很迷糊了 Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节。以此类推,最后不足6Bit的部分用0补足。
之后,每个8Bit的部分对应码表转换成ASIC字符。多余的部分用'='补齐,使编码结果为4的整数倍。
参考:[url]http://www.luocong.com/articles/show_article.asp?Article_ID=17[/url]
[url]http://www.5dmail.net/html/2004-1-30/200413084348.htm[/url]
页:
[1]