下载此文档

Lua小程序 十六进制字符串和二进制数据间的转换.doc


文档分类:IT计算机 | 页数:约5页 举报非法文档有奖
1/5
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/5 下载此文档
文档列表 文档介绍
Lua小程序十六进制字符串和二进制数据间的转换
从十六进制字符串转为二进制数:
1 #! /usr/local/bin/lua
2
3
4 --Note: Input hex string file's content must be
5 --a upper case hex string
6
7 --Check the arguments
8 if #arg ~= 2 then
9 print("Usage: <Input Hex String File> <Output Bytes File>\n");
10 ();
11 end
12
13 --Open input hex string file
14 local hexstrfile = (arg[1], "rb");
15 if nil == hexstrfile then
16 print("Can not open input file!\n");
17 ();
18 end
19
20 --Read from the hex string file
21 local hexstr = hexstrfile:read("*a");
22 if nil == hexstr then
23 print("Can not read hex string from input file!\n");
24 elseif 1 == (hexstr)%2 then
25 print("Hex string size must be evel!\n");
26 ();
27 else
28 print("Hex String:", hexstr, "\n");
29 end
30
31 --Close hex string file
32 hexstrfile:close();
33
34 --Open output binary file
35 local bytesfile = (arg[2], "wb");
36 if nil == bytesfile then
37 print("Can not open output file to write!\n");
38 ();
39 end
40
41 --Convert hex string to bytes
42 for i = 1, (hexstr) - 1, 2 do
43 local doublebytestr = (hexstr, i, i+1);
44 local n = tonumber(doublebytestr, 16);
45 if 0 == n then
46 bytesfile:write('\00');
47 else
48 bytesfile:write(("%c", n));
49 end
50 end
51
52 --Close output binary file
53

Lua小程序 十六进制字符串和二进制数据间的转换 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数5
  • 收藏数0 收藏
  • 顶次数0
  • 上传人坐水行舟
  • 文件大小57 KB
  • 时间2018-09-28