Lua小程序 十六进制字符串和二进制数据间的转换.pdf
《Lua小程序 十六进制字符串和二进制数据间的转换.pdf》由会员分享,可在线阅读,更多相关《Lua小程序 十六进制字符串和二进制数据间的转换.pdf(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Lua 小程序 十六进制字符串和二进制数据间的转换从十六进制字符串转为二进制数:1#!/usr/local/bin/lua234-Note:Input hex string files content must be5-a upper case hex string67-Check the arguments8 if#arg=2 then9print(Usage:hexstr2bytes.lua n);10os.exit();11 end1213-Open input hex string file14 local hexstrfile=io.open(arg1,rb);15 if nil=h
2、exstrfile then16print(Can not open input file!n);17os.exit();18 end1920-Read from the hex string file21 local hexstr=hexstrfile:read(*a);22 if nil=hexstr then23print(Can not read hex string from input file!n);24 elseif 1=string.len(hexstr)%2 then25print(Hex string size must be evel!n);26os.exit();27
3、 else28print(Hex String:,hexstr,n);29 end3031-Close hex string file32 hexstrfile:close();3334-Open output binary file35 local bytesfile=io.open(arg2,wb);36 if nil=bytesfile then37print(Can not open output file to write!n);38os.exit();39 end4041-Convert hex string to bytes42 for i=1,string.len(hexstr
4、)-1,2 do43local doublebytestr=string.sub(hexstr,i,i+1);44local n=tonumber(doublebytestr,16);45if 0=n then46bytesfile:write(00);47else48bytesfile:write(string.format(%c,n);49end50 end5152-Close output binary file53 bytesfile:close();545556从二进制数转十六进制字符串:1#!/usr/local/bin/lua234-Check the arguments5 if
5、#arg=2 then6print(Usage:bytes2hexstr.lua n);7os.exit();8 end910-Open input bytes file11 local bytesfile=io.open(arg1,rb);12 if nil=bytesfile then13print(Can not open input file!n);14os.exit();15 end1617-Read from the bytes file18 local bytes=bytesfile:read(*a);19 if nil=bytes then20print(Can not rea
6、d bytes from input file!n);21os.exit();22 end2324-Close bytes file25 bytesfile:close();2627-Open output hex string file28 local hexstrfile=io.open(arg2,wb);29 if nil=hexstrfile then30print(Can not open output file to write!n);31os.exit();32 end3334-Convert bytes to hex string35 for i=1,string.len(by
7、tes)do36local charcode=tonumber(string.byte(bytes,i,i);37local hexstr=string.format(%02X,charcode);38hexstrfile:write(hexstr);39 end4041-Close output hex string file42 hexstrfile:close();使用 Crypto+生成的私钥文件(使用 HexEncoder 编码的 DER 格式文件)ch:30820275020100300D06092A864886F70D01010105000482025F3082025B02010
8、0028181009EEA4CF16CCD0DEF5FAA7B27D872AA0B5CCC286464E5338AE958E076BE764CA97C52E89D52329A4BB10DD586031CDD71D0C0581A8A3A41378AED44AA4F25B09C8BF2C573C882BD82A8EE670C49229C609FB740CC7230FD25411851B3A37AEF91AA6EF3D2EF2CEC5E03A04257D2C3FDDAAD092696A96B7D0358B1AE339625F9B50201110281802EBD61ECA7879AAFD0D7C9D
9、E8AF48C5DB1E1B186F07FA5BF71CED89B6531DA4FF763ADD3EAFFD307341320AEF1DB503088B10ADAA120314C9245C8E6CBFC06C42AE3AFD1612DBA9FC6619E5E08E70BA0CB211D73FD94EA35D3D7BDFCA8AFDC98F5E1277FB1C12ACF0975D3AA74227B6D23A692DF5B9E6720C5B81578CBBA3D2D024100D1FD16E7CB78989785B87DF9E80D071AF95AC47B70FD053A8D03784FF46D6
10、484EEB09B200C3800FBE46C02A20127BED5CC880F0BAD8FDC9408FAE7A3C634C2C3024100C1BC58C4190843FF1A5068390F9DA0EFC0857E936FD342007D6A86A23E51369E448D9F33ECFDC03BCBD6D67246C09B5E9A7DF0F9F78DDB664911B0C1E8119A2702403DC2E89E8723782C90BDCAB2E9E5B6CBB2C057E8122C4CD4FC4C508FFC98A51809F7B5278B1F87D19D892DF369C0654
11、DF0DCB9218D668C2B8A2BAD8A85970C1B024100B656EA21F97130F018C425DB59FDC4A569E70DB7F0C6D4B52ABE9CD4EF5B7EB313581D5E0C3A1E56657EE7F31569FBA4552B3D277F94924226A73CF2BC4CCD51024100BB5DA6433C896D07C88821107EF658DD201B7049191213A2001D3EC0A68279CCC6A36EC5DB384F2797CA47F94505A069B247758D8E6D1A8D30B068BCD26FC7F
12、6使用 hexstr2bytes.lua:hexstr2bytes.lua ch chout生成 chout:00000000h:30 82 02 75 02 01 00 30 0D 06 09 2A 86 48 86 F7;0?u0*咹嗺00000010h:0D 01 01 01 05 00 04 82 02 5F 30 82 02 5B 02 01;.?_0?.00000020h:00 02 81 81 00 9E EA 4C F1 6C CD 0D EF 5F AA 7B;.亖.炾 L 駆?颻獅00000030h:27 D8 72 AA 0B 5C CC 28 64 64 E5 33 8
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Lua小程序 十六进制字符串和二进制数据间的转换 Lua 程序 十六进制 字符串 二进制 数据 转换
限制150内