用Python创建bin文件并查看

使用pycharm写一个创建bin文件的脚本

import numpy as np

# 数据
data = np.array([0x33, 0x34, 0x34, 0x34], dtype=np.uint8)

# 指定文件路径
file_path = (r'C:\Users\JK\Desktop\example1.bin')

# 保存为bin格式
with open(file_path, 'wb') as file:
    data.tofile(file)

print("ok")

用vscode查看

使用vscode以16进制方式查看bin文件内容_vscode打开bin文件-优快云博客

安装Hex Editor,插件,快捷键ctrl+shift+p,输入hex。可以查看文件的内容

注:下面补充一个自动创建bin文件的gui的界面

import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, QMessageBox
# gui界面,创建bin文件
class BinFileCreator(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 创建布局
        layout = QVBoxLayout()

        # 创建内容输入框
        self.content_label = QLabel('BIN文件内容(十六进制,用空格分隔):')
        self.content_input = QLineEdit()
        layout.addWidget(self.content_label)
        layout.addWidget(self.content_input)

        # 创建文件路径选择按钮
        self.path_label = QLabel('文件路径:')
        self.path_input = QLineEdit()
        self.browse_button = QPushButton('浏览...')
        self.browse_button.clicked.connect(self.browse_folder)
        path_layout = QHBoxLayout()
        path_layout.addWidget(self.path_input)
        path_layout.addWidget(self.browse_button)
        layout.addWidget(self.path_label)
        layout.addLayout(path_layout)

        # 创建文件名输入框
        self.filename_label = QLabel('文件名:')
        self.filename_input = QLineEdit()
        layout.addWidget(self.filename_label)
        layout.addWidget(self.filename_input)

        # 创建确认按钮和清空按钮
        self.create_button = QPushButton('确认')
        self.create_button.clicked.connect(self.create_bin_file)
        self.clear_button = QPushButton('清空')
        self.clear_button.clicked.connect(self.clear_inputs)
        button_layout = QHBoxLayout()
        button_layout.addWidget(self.create_button)
        button_layout.addWidget(self.clear_button)
        layout.addLayout(button_layout)

        # 设置窗口布局
        self.setLayout(layout)
        self.setWindowTitle('BIN文件创建器')
        self.setGeometry(100, 100, 400, 200)

    def browse_folder(self):
        folder_path = QFileDialog.getExistingDirectory(self, "选择文件夹")
        if folder_path:
            self.path_input.setText(folder_path)

    def create_bin_file(self):
        content = self.content_input.text().strip()
        file_path = self.path_input.text().strip()
        filename = self.filename_input.text().strip()

        if not content or not file_path or not filename:
            QMessageBox.warning(self, '警告', '请填写所有字段')
            return

        try:
            # 将输入的内容转换为numpy数组
            data = np.array([int(x, 16) for x in content.split()], dtype=np.uint8)
        except ValueError:
            QMessageBox.warning(self, '警告', '请输入有效的十六进制数')
            return

        # 保存为bin格式
        full_path = f'{file_path}/{filename}.bin'
        with open(full_path, 'wb') as file:
            data.tofile(file)

        QMessageBox.information(self, '成功', f'文件已保存到 {full_path}')

    def clear_inputs(self):
        self.content_input.clear()
        # self.path_input.clear()
        # self.filename_input.clear()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = BinFileCreator()
    ex.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值