当前位置: 首页 > news >正文

Base64 编码 lua

8e02f9ead6894ab7b4822708ea240f31.jpg

 

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)

http://www.lryc.cn/news/306882.html

相关文章:

  • 41.仿简道云公式函数实战-数学函数-SUMIF
  • 挑战30天学完Python:Day22 爬虫
  • AI:138-开发一种能够自动化生成艺术品描述的人工智能系统
  • 智慧城市建设的新里程碑:公共服务电子支付大屏
  • Netty之Decoder详解与实战
  • PCIe P2P DMA全景解读
  • 【Git】window下大小写不敏感问题处理
  • 【JS】【Vue3】【React】获取滚轮位置的方法:JavaScript、Vue 3和React示例
  • 什么是线程和进程?
  • MaxScale实现mysql8读写分离
  • 【c语言】内存函数
  • 规则引擎项目
  • Docker Image(镜像)
  • qgis启动提示Could not load qgis_app.dll
  • 数据分析---Python与sql
  • 【Oracle】玩转Oracle数据库(六):模式对象管理与安全管理
  • 微服务篇之限流
  • react脚手架
  • 【Vue3】插槽使用和animate使用
  • HarmonyOS—低代码开发Demo示例
  • Spring体系下解决请求统一加解密之ResponseBodyAdvice和RequestBodyAdvice
  • C# 经典:ref 和 out 的区别详解
  • Linux 系统添加虚拟内存的方法
  • PHP 函数四
  • 【Android】反编译APK及重新打包
  • 下载huggingface数据集到本地并读取.arrow文件遇到的问题
  • .NET高级面试指南专题十一【 设计模式介绍,为什么要用设计模式】
  • 【Web】关于jQuery萌新必须要知道的那些基础知识
  • 第 1 章 微信小程序与云开发从入门到实践从零开始做小程序——开发认识微信小程序
  • 数据隐私安全趋势