将bin文件以16进制字符显示

本文介绍了在Windows 10中如何使用Format-Hex工具和Python3脚本来将.bin文件转换为16进制显示,以便于处理乱码问题,包括两种方法的操作步骤和适用场景。

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

在win10中在处理.bin文件时,用普通的文本编辑器打开会乱码,故需将其转成16进制显示

1. 第一种方法(利用win10自带的Format-Hex工具)

win10同时按下win+R,输入powershell,进到powershell终端(不是cmd终端,powershell可以理解为是cmd终端的进阶版),输入命令Format-hex -Path ./xxx.bin即可
此方法不需要装任何软件,但有一个弊端,当.bin文件过大时,.bin文件不能在powershell终端完全显示

2. 第二种方法(利用python3处理.bin文件)

为了能将.bin文件显示完全,自己写了一个python处理程序,名称为 bin_to_hex.py ,运行该程序首先得在自己电脑上安装python3的运行环境,在cmd终端通过python bin_to_hex.py xxx.bin 128命令将.bin文件转成16进制字符显示的xxx.txt文件

# -*- coding:utf-8 -*-
import sys
import os

filepath = "HDCPTX_WVL1_HDMITX_3KEY.bin" #要处理的bin文件
show_number = 16 #每行显示的16进制字符数,默认为16

if len(sys.argv) == 3: #读取输入参数
    filepath = sys.argv[1]
    show_number = int(sys.argv[2])

print(filepath + " will be converted to HEX file!\n")

binfile = open(filepath, 'rb')
hexfilepath = os.path.splitext(filepath)[0] + ".txt" #保留原文件名
hexfile = open(hexfilepath, 'w+')

ch = binfile.read(1) #每次读取一个字节
i = 0 #总共要处理的字符数
count = 0 #每行的前缀地址

while ch:
    '''这一版本直接显示16进制字符'''
    '''
    data = hex(ord(ch))[2:] #ord转成ascii码的十进制,再通过hex转成16进制,通过[2:]去掉"0x"
    #print(data)
    txt_data = data.zfill(2) #前面补0
    
    i = i + 1
    if i % show_number == 1:
        hexfile.write("\n")
        count_str = hex(count)[2:]
        txt_count = count_str.zfill(6) #前面补0
        hexfile.write(txt_count) #前缀地址
        hexfile.write('   ')
        count = count + show_number
        hexfile.write(txt_data)
        hexfile.write(' ')
    else:
        hexfile.write(txt_data)
        hexfile.write(' ')
    ch = binfile.read(1)
    '''
    
    '''在每一行的后面增加显示ascll字符'''
    str_list = [] #该列表用于暂存每行要显示的字符
    count_str = hex(count)[2:] #通过hex转成16进制,通过[2:]去掉"0x"
    txt_count = count_str.zfill(6) #前面补0
    str_list.insert(0, txt_count+'   ') #前缀地址
    count = count + show_number #前缀地址更新
    str_list.insert(show_number, '   ') #16进制字符和ascii字符之间的空隙
    for i_list in range(show_number):
        if len(ch) != 0:
            data = hex(ord(ch))[2:] #ord转成ascii码的十进制,再通过hex转成16进制,通过[2:]去掉"0x"
            txt_data = data.zfill(2) #前面补0
            str_list.insert(1+i_list, txt_data+' ')
            int_ascii = ord(ch)
            if int_ascii < 65 or (int_ascii > 90 and int_ascii < 97) or int_ascii > 122: #只将字母打印出来,其他的打印空格
                int_ascii = 32
            str_list.insert(1+show_number+i_list+1, chr(int_ascii))
            ch = binfile.read(1)
        else:
            list_length = int((len(str_list)-2)/2)
            for i_list_left in range(show_number - list_length): #不足show_number时补空格
                str_list.insert(1+list_length+i_list_left, '   ')
            break
    str_list.append('\n')
    #print(str_list)
    for str_text in str_list: #将字符串list写入txt文件
        hexfile.write(str_text)

print(hexfilepath + " is the HEX file!")

binfile.close()
hexfile.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值