2023年12月2日发(作者:)
将bin文件以16进制字符显示在win10中在处理.bin文件时,用普通的文本编辑器打开会乱码,故需将其转成16进制显示1. 第一种方法(利用win10自带的Format-Hex工具)win10同时按下win+R,输入powershell,进到powershell终端(不是cmd终端,powershell可以理解为是cmd终端的进阶版),输入命令Format-hex -Path ./即可此方法不需要装任何软件,但有一个弊端,当.bin文件过大时,.bin文件不能在powershell终端完全显示2. 第二种方法(利用python3处理.bin文件)为了能将.bin文件显示完全,自己写了一个python处理程序,名称为
bin_to_ ,运行该程序首先得在自己电脑上安装python3的运行环境,在cmd终端通过python bin_to_ 128命令将.bin文件转成16进制字符显示的文件# -*- coding:utf-8 -*-import sysimport osfilepath = "HDCPTX_WVL1_HDMITX_" #要处理的bin文件show_number = 16 #每行显示的16进制字符数,默认为16if len() == 3: #读取输入参数 filepath = [1] show_number = int([2])print(filepath + " will be converted to HEX file!n")binfile = open(filepath, 'rb')hexfilepath = xt(filepath)[0] + ".txt" #保留原文件名hexfile = open(hexfilepath, 'w+')ch = (1) #每次读取一个字节i = 0 #总共要处理的字符数count = 0 #每行的前缀地址while ch: '''这一版本直接显示16进制字符''' ''' data = hex(ord(ch))[2:] #ord转成ascii码的十进制,再通过hex转成16进制,通过[2:]去掉"0x" #print(data) txt_data = (2) #前面补0
i = i + 1 if i % show_number == 1: ("n") count_str = hex(count)[2:] txt_count = count_(6) #前面补0 (txt_count) #前缀地址 (' ') count = count + show_number (txt_data) (' ') else: (txt_data) (' ') ch = (1) '''
'''在每一行的后面增加显示ascll字符''' str_list = [] #该列表用于暂存每行要显示的字符 count_str = hex(count)[2:] #通过hex转成16进制,通过[2:]去掉"0x" txt_count = count_(6) #前面补0 str_(0, txt_count+' ') #前缀地址 count = count + show_number #前缀地址更新 str_(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 = (2) #前面补0 str_(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_(1+show_number+i_list+1, chr(int_ascii)) ch = (1) else: list_length = int((len(str_list)-2)/2) for i_list_left in range(show_number - list_length): #不足show_number时补空格 str_(1+list_length+i_list_left, ' ') break str_('n') #print(str_list) for str_text in str_list: #将字符串list写入txt文件 (str_text)print(hexfilepath + " is the HEX file!")()()


发布评论