概述
下面的Python程序可以实现将一个二进制文件转换为C语言的数组格式:
#!/usr/bin/python
import sys
DEBUG = False
def printline():
print '-'*80
def ShowCmdArgs():
i = 0
for arg in sys.argv:
print 'arg', i, ':', arg
i = i + 1
def usage():
progname = sys.argv[0]
printline()
print 'Usage:'
print 'bin2C.py -i binfilename -o cfilename'
printline()
def readbin(filename=''):
if filename == '':
return ''
else:
lines = ''
try:
fd = open(filename, 'rb')
except:
print 'Open ' + filename + ' Fail!'
return ''
else:
buff = fd.read()
fd.close()
return buff
def main():
iname = ''
oname = ''
if DEBUG:
ShowCmdArgs()
if len(sys.argv) == 3:
if sys.argv[1] == '-i':
iname = sys.argv[2]
else:
usage()
return
elif len(sys.argv) == 5:
if sys.argv[1] == '-i':
iname = sys.argv[2]
if sys.argv[3] == '-o':
oname = sys.argv[4]
else:
usage()
return
elif sys.argv[1] == '-o':
oname = sys.argv[2]
if sys.argv[3] == '-i':
iname = sys.argv[4]
else:
usage()
return
else:
usage()
return
else:
usage()
return
if iname != '':
print 'In: ' + iname
if oname != '':
print 'Out: ' + oname
ibuff = readbin(iname)
#print ibuff
keystring = ''
keyidx = 0
for key in ibuff:
if ((keyidx + 1) % 8) == 0:
keystring = keystring + '0x%02x' % ord(key)
keystring = keystring + 'n'
else:
keystring = keystring + '0x%02x ' % ord(key)
keyidx = keyidx + 1
if oname == '':
print keystring
else:
try:
fd = open(oname, 'w')
except:
print 'Open ' + oname + ' Fail'
else:
fd.write(keystring)
fd.close()
print 'Tatol len: ' + str(keyidx)
main()
代码写得比较匆忙,但经验证可用。
顺便贴出一个base64格式的pem文件转换为C格式的pyhon实现:
#!/usr/bin/python
import base64
import sys
DEBUG = False
def printline():
print '-'*80
def ShowCmdArgs():
i = 0
for arg in sys.argv:
print 'arg', i, ':', arg
i = i + 1
def usage():
progname = sys.argv[0]
printline()
print 'Usage:'
print 'pem2C.py -i pemfilename -o cfilename'
printline()
def readpem(filename=''):
if filename == '':
return ''
else:
lines = ''
linenum = 0
try:
fd = open(filename, 'r')
except:
print 'Open ' + filename + ' Fail!'
return ''
else:
for line in fd:
if '---' in line:
pass
else:
for ch in line:
if ch == 'n' or ch == 'r':
pass
else:
lines = lines + ch
fd.close()
return lines
def main():
iname = ''
oname = ''
if DEBUG:
ShowCmdArgs()
if len(sys.argv) == 3:
if sys.argv[1] == '-i':
iname = sys.argv[2]
else:
usage()
return
elif len(sys.argv) == 5:
if sys.argv[1] == '-i':
iname = sys.argv[2]
if sys.argv[3] == '-o':
oname = sys.argv[4]
else:
usage()
return
elif sys.argv[1] == '-o':
oname = sys.argv[2]
if sys.argv[3] == '-i':
iname = sys.argv[4]
else:
usage()
return
else:
usage()
return
else:
usage()
return
if iname != '':
print 'In: ' + iname
if oname != '':
print 'Out: ' + oname
pem = readpem(iname)
print pem
keys = base64.b64decode(pem)
keystring = ''
keyidx = 0
for key in keys:
if ((keyidx + 1) % 8) == 0:
keystring = keystring + '0x%02x' % ord(key)
keystring = keystring + 'n'
else:
keystring = keystring + '0x%02x ' % ord(key)
keyidx = keyidx + 1
if oname == '':
print keystring
else:
try:
fd = open(oname, 'w')
except:
print 'Open ' + oname + ' Fail'
else:
fd.write(keystring)
fd.close()
print 'Tatol len: ' + str(keyidx)
main()
此程序可以将BASE64编码格式的pem文件或crt文件转换为二进制,然后在转换为C语言数值格式。
最后
以上就是合适毛豆为你收集整理的二进制转换为C语言数组-Python实现的全部内容,希望文章能够帮你解决二进制转换为C语言数组-Python实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复