bin2hex.py && hex2bin.py

本文介绍了一个简单的Python脚本工具,用于将十六进制文件转换为二进制格式。该工具通过逐字节读取输入文件并转换为二进制输出,适用于逆向工程或数据格式转换等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python import sys fd = open(sys.argv[1], "rb") data = fd.read(1024) cnt = 0 while len(data): for c in data: if (cnt%16 == 0): print "%08x "%cnt, hex = ord(c) print "%02x"%(hex), if (cnt%16 == 15): print "" cnt += 1 data = fd.read(1024)

hex2bin.py:

#!/usr/bin/env python import sys import binascii fd = open(sys.argv[1], "r") lines = fd.readlines() func = lambda e:binascii.a2b_hex(e) for l in lines: colon_index = 0 try: colon_index = l.index(':') l = l[colon_index+1:] except: colon_index = 0 elems = l.strip().split() for e in elems: hex = func(e) #print hex, sys.stdout.write(hex) fd.close()

使用方法:

python hex2bin.py file > output_file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值