将二进制文件 (.bin) 转换为 ASCII 十六进制文本文件的工具。支持多种数据宽度、字节序和格式选项。
作者: GuoHowe 版本: 1.0.0
- ✅ 支持多种数据宽度: 1/2/4/8 字节
- ✅ 支持大端/小端字节序选择
- ✅ 支持大写/小写十六进制输出
- ✅ 自定义每行字节数
- ✅ 拖放文件支持
- ✅ 实时进度显示
- ✅ 转换结果预览
- ✅ 独立运行或集成到主工程
- ✅ 命令行参数支持
独立运行:
python bin_hex_converter_dialog.py指定初始文件:
python bin_hex_converter_dialog.py input.bin使用核心转换库:
python -c "from bin_hex_converter_project.core.format_converter import bin_to_hex; bin_to_hex('input.bin', 'output.txt')"from bin_hex_converter_project.core.format_converter import BinHexConverter
# 创建转换器实例
converter = BinHexConverter()
# 执行转换
converter.convert(
input_file='firmware.bin',
output_file='firmware_hex.txt',
data_width=4, # 4 字节 (Word)
bytes_per_row=16, # 每行 16 字节
byteorder='little', # 小端
uppercase=True # 大写
)在主工程中动态导入:
try:
from bin_hex_converter_project.bin_hex_converter_dialog import BinHexConverterDialog
dialog = BinHexConverterDialog(parent=self)
dialog.exec_()
except ImportError:
QMessageBox.warning(self, "警告", "Bin to Hex 转换器未集成")1: 按字节显示 (Byte)2: 按半字显示 (Half-Word, 16位)4: 按字显示 (Word, 32位)8: 按双字显示 (Double-Word, 64位)
'little': 小端序 (低位字节在前)'big': 大端序 (高位字节在前)
控制输出文件每行显示的字节数,建议值: 8, 16, 32
True: 大写 (例:ABCD1234)False: 小写 (例:abcd1234)
输入文件 test.bin (4 字节):
0x12 0x34 0x56 0x78
转换参数:
- 数据宽度: 1 字节
- 每行字节数: 4
- 字节序: 小端
- 大小写: 大写
输出 test_HEX.txt:
12 34 56 78
输入文件 test.bin (8 字节):
0x12 0x34 0x56 0x78 0x9A 0xBC 0xDE 0xF0
转换参数:
- 数据宽度: 4 字节
- 每行字节数: 8
- 字节序: 小端
- 大小写: 大写
输出 test_HEX.txt:
78563412 F0DEBC9A
同样输入,但字节序改为大端:
输出:
12345678 9ABCDEF0
bin_hex_converter_project/
├── __init__.py # 包初始化
├── bin_hex_converter_dialog.py # 主对话框 (入口)
├── bin_hex_converter_dialog.spec # PyInstaller 配置
├── README.md # 本文档
├── core/ # 核心功能
│ ├── __init__.py
│ └── format_converter.py # 转换器核心类
├── widgets/ # UI 组件
│ ├── __init__.py
│ ├── widgets.py # 自定义控件
│ └── utils.py # 资源路径工具
└── resources/ # 资源文件
└── HOWE_LOGO.ico # 应用图标
pyinstaller bin_hex_converter_dialog.spec生成的可执行文件位于 dist/BinHexConverter.exe
- 将项目目录放置在主工程同级目录
- 在主工程的
main.spec中添加:
# 检测 bin_hex_converter_project
binhex_dir = os.path.abspath(os.path.join('..', 'bin_hex_converter_project'))
bundle_binhex = os.path.exists(binhex_dir)
if bundle_binhex:
# 添加到路径
pathex.append(os.path.dirname(spec_dir))
# 添加隐式导入
hiddenimports.extend([
'bin_hex_converter_project',
'bin_hex_converter_project.bin_hex_converter_dialog',
])
# 收集文件
for root, dirs, files in os.walk(binhex_dir):
# 排除不需要的目录
dirs[:] = [d for d in dirs if d not in ['dist', 'build', '__pycache__', '.git']]
for file in files:
if file.endswith(('.py', '.ico')):
src = os.path.join(root, file)
dst = os.path.relpath(root, os.path.dirname(spec_dir))
datas.append((src, dst))- 在主应用中添加菜单入口:
def open_bin_hex_converter(self):
"""打开 Bin to Hex 转换器"""
try:
from bin_hex_converter_project.bin_hex_converter_dialog import BinHexConverterDialog
dialog = BinHexConverterDialog(parent=self)
dialog.exec_()
except ImportError as e:
QMessageBox.warning(self, "警告", "Bin to Hex 转换器未集成")- Python 3.8+
- PyQt5 >= 5.15
安装依赖:
pip install PyQt5本项目使用多重导入策略,支持:
- 作为包内模块导入 (相对导入)
- 独立脚本运行 (绝对导入)
- 集成到主工程 (动态导入)
- 大文件转换会占用内存,建议分批处理超大文件 (>500MB)
- 转换过程在后台线程执行,不会阻塞界面
- 如果文件大小不是数据宽度的整数倍,末尾数据会用
0xFF填充
- 小端: 低位字节在前,常用于 x86/ARM 架构
- 大端: 高位字节在前,常用于网络协议和某些 MCU
A: 可以减小每行字节数,或者使用更大的数据宽度来减少空格数量。
A: 根据目标芯片架构决定。可以先尝试小端,如果显示异常再改为大端。
A: 当前版本输出的是纯 ASCII 十六进制文本,不是 Intel HEX 格式。如需 Intel HEX,可以后续扩展。
- 初始版本发布
- 支持基本的 Bin to Hex 转换
- 实现拖放界面
- 支持多种参数配置
- 支持进度显示和预览
本项目遵循内部使用协议,版权归 GuoHowe 所有。
如有问题或建议,请联系开发者。
Happy Converting! 🚀