lua语言中十六进制字符串转换为二进制字符串的方法
2016-07-06 23:11
501 查看
今天在用openresty处理AES解密的时候,发现lua-resty-string插件中aes.lua的函数decrypt(str)必须保证是二进制字符串,而针对十六进制字符串则无法解析,上网找了半天,发现很多十六进制转二进制字符串的函数其实效果都不好,结合他们的函数自己修改了一下。function hex2bin( hexstr )
local str = ""
for i = 1, string.len(hexstr) - 1, 2 do
local doublebytestr = string.sub(hexstr, i, i+1);
local n = tonumber(doublebytestr, 16);
if 0 == n then
str = str .. '\00'
else
str = str .. string.format("%c", n)
end
end
return str
end
这段函数还有改进的地方,有时间可以想想怎么依靠ffi来处理,效率更高
local str = ""
for i = 1, string.len(hexstr) - 1, 2 do
local doublebytestr = string.sub(hexstr, i, i+1);
local n = tonumber(doublebytestr, 16);
if 0 == n then
str = str .. '\00'
else
str = str .. string.format("%c", n)
end
end
return str
end
这段函数还有改进的地方,有时间可以想想怎么依靠ffi来处理,效率更高
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- 用ASP编写的加密和解密类
- 解密网页加密的两个方法
- 详解Lua中的表的概念及其相关操作方法
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- Lua编程示例(二):面向对象、metatable对表进行扩展
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记