首页 > 编程知识 正文

python字符串截取,大小端转换软件

时间:2023-05-04 06:15:14 阅读:168771 作者:160

用4个字节转换. bin文件的大小,生成. I文件。

导入结构

导入系统

import os

src _ file=' c :_ users\ Xiaoli gen\ desktopft 8006 _ pram boot _ 2017 05 12 _ app.bin '

#主体

if sys.argv.__len__ () 1:

#获取参数

src_file=sys.argv[1]

else:

#文本输入

src _ file=input (source binpath : ) )。

ifnotOS.path.exists(src_file ) :

打印(' file path invalid! Exiting . ' )

exit(1)。

dst _ file=src _ file [ 0: ] (src _ file._ len _ ((-4 ) ) ) )

dst_i_file=dst_file

dst_file ='_le.bin '

dst_i_file ='.i '

源文件: (0) ntargetfile:(1(n(t )2) ).format(src_file,dst_file,DST_I_ ) )

try:

SF=open(src_file,' rb ' ) ) )。

df=open(dst_file,' wb ' ) ) )。

dif=open(dst_I_file,' w ' )

buf _ tmp=[ b '0' forxinrange (0,4 ) ]

contents=sf.read (

buf_size=contents.__len__ ()

小于#4字节,自动补0

extra_size=(buf_size % 4)

if extra_size 0:

buf_size=(4-extra_size ) ) ) )。

contents=contents b'0000 '

forIinrange(0,buf_size,4 ) :

buf_tmp[3]=contents[i]

buf_tmp[2]=contents[i 1]

buf_tmp[1]=contents[i 2]

buf_tmp[0]=contents[i 3]

if(I0 ) and ) ) I )==0) :

dif.write('n ' )。

forjinrange (0,4 ) :

dif.write(str.format('0xx,' % buf_tmp[j] ) )

# pack into bytes flow

TMP_bytes=struct.pack('4b ',buf_tmp[0],buf_tmp[1],buf_tmp[2],buf_tmp[3] )

df.write(tmp_bytes ) )。

finally:

if dif:

dif.close () )

if sf:

sf.close () )

if df:

df.close () )

打印(错误计数! 转换失败! ' )

exit(1)。

打印(' convert completed!' )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。