Base64 编码 lua

Base64 编码

-- Base64 字符表

local base64_chars = {

'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',

'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'

}

-- Base64 编码函数

function base64_encode (data)

local bits = 0

local cur = 1

local result = {}

for i = 1, #data do

local byte = string.byte (data, i)

bits = bits | (byte << (cur * 8))

cur = cur + 1

if cur == 3 then

for j = 18, -1, -6 do

table.insert (result, base64_chars [1 + (bits>> j) & 0x3F])

end

cur = 0

bits = 0

end

end

if cur > 0 then

for j = (cur * 8), 5, -6 do

table.insert (result, base64_chars [1 + (bits>> j) & 0x3F])

end

while #result % 4 ~= 0 do

table.insert (result, '=')

end

end

return table.concat (result)

end

-- 要编码的字符串

local long_string = "这是一个需要编码的长字符串。"

-- 编码字符串

local encoded_string = base64_encode (long_string)

print ("Base64 编码后的字符串:", encoded_string)

相关推荐
Syntech_Wuz13 小时前
从 C 到 C++:容器适配器 std::stack 与 std::queue 详解
数据结构·c++·容器··队列
wanhengidc14 小时前
巨椰云手机引领未来
运维·服务器·网络·游戏·智能手机
wanhengidc14 小时前
云手机的真实体验感怎么样
运维·服务器·安全·游戏·智能手机
艾莉丝努力练剑15 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
wanhengidc15 小时前
云手机 流畅运行
运维·服务器·安全·游戏·智能手机
小此方15 小时前
C语言自定义变量类型结构体理论:从初见到精通(下)
c语言·数据结构·算法
im_AMBER15 小时前
数据结构 05 栈和队列
数据结构·笔记·学习
2401_8658548816 小时前
腾讯云的游戏盾怎么样
游戏·云计算·腾讯云
_poplar_16 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
da_vinci_x16 小时前
告别“手绘序列帧”:Substance Designer中的程序化VFX材质工作流
游戏·材质·贴图·技术美术·游戏美术·vfx·substance designer