代码如下:
import sys Numbers_In_Line = 16 #一行有几个数字 ''' * 将一个文件转换为二进制文件(binary) * python3
tran_to_binary.py input_file out_file * 20190713 ''' class TranStrToBinary():
def tran_string_to_binary(self,buf): bytes_str = bytes(buf, encoding='utf-8')
strbuf = "" for i in range(0,len(bytes_str)): tmp_str = str(hex(bytes_str[i]))
tmp_str = tmp_str[2:].rstrip() if len(tmp_str) < 2: strbuf += "0" + tmp_str + "
" else: strbuf += tmp_str + " " num = Numbers_In_Line * 3 listBuf =
list(strbuf) line_count = len(listBuf) if line_count / num > 0: for i in
range(0,int(line_count / num)): pos = (i + 1) * num listBuf[pos-1] = '\n'
strbuf = "".join(listBuf) return strbuf.rstrip() def
tran_binary(self,inputFile,outputFile): try: with open(inputFile,"r") as fp_r:
bufs = fp_r.read() except FileNotFoundError: print("The input files is not
exist !") exit(2) print(self.tran_string_to_binary(bufs)) with
open(outputFile,"w") as fp_w: fp_w.write(self.tran_string_to_binary(bufs)) if
__name__ == '__main__': print(str(sys.argv[0]) + " enter") tstb =
TranStrToBinary() if len(sys.argv) != 3: print("The Bad Parameters !") exit(1)
tstb.tran_binary(sys.argv[1],sys.argv[2])

技术
©2019-2020 Toolsou All rights reserved,
车主无忧:为什么放弃开源Kafka?必传之作!Alibaba内部出品Redis深度笔记及源码宝典统信UOS首次公布软件适配:QQ、微信、迅雷都有了阿里开发10年技术核心总结,Springboot+Redis文档,送给努力上进的程序员python画爱心Docker容器数据卷详解(共享数据)为何华为的5G专利高居第一名,却还被高通要求缴纳专利费?Spring循环依赖三级缓存是否可以减少为二级缓存?C#中字典的排序方法面试还不会Spring?阿里P8总结的100道面试解析,让你实锤面试官