当前位置: 移动技术网 > IT编程>脚本编程>Python > python 处理string到hex脚本的方法

python 处理string到hex脚本的方法

2019年06月15日  | 移动技术网IT编程  | 我要评论

潘映竹,冷情皇子俏皇妃,龙之梦影城

实现目标:把文件1中数据如:b4a6c0ed69 处理后放入文件2:0xb4, 0xa6, 0xc0, 0xed, 0x69

v1.0代码如下(后续继续优化):

#!/usr/bin/env python
# -*- coding:utf-8 -*- 
from sys import argv 
script,first = argv 
 
buf = []
tmp = []
 
#读取待处理文件全部内容 并存到buf中
with open(first, 'r') as f:
 buf = f.read()
f.closed
 
#对buf中内容,进行每隔2个字符取出,并以", 0x"连接,最后在头部加上'0x'
for i in range(0,len(buf),2): 
 tmp.append(buf[i:i+2])
hex_temp = ", 0x".join(tmp)
hex_buf = '%s%s' %('0x', hex_temp)
 
#把处理后的hex数据写入到hex.txt文件中
with open("hex.txt", 'w') as out:
 out.write(hex_buf)
out.close()

执行过程(注意用命令行输入文件1参数的形式):

python 处理string到hex

输出结果:

python 处理string到hex

以上这篇python 处理string到hex脚本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网