【Python】二进制转ASCII码小脚本

#coding:utf-8

#developed by carrypan!

import binascii
import optparse

def main():
    usage="%prog -b <target ascii>"
    parser=optparse.OptionParser(usage)
    parser.add_option('-b',dest='tgtascii',type='string',help='target ascii with quotes(without 0b)')
    (options,args)=parser.parse_args()
    binToAsc(options.tgtascii)

def binToAsc(tgtasc):
    try:
        bin_to_dec=int(tgtasc,2)
        dec_to_asc=chr(bin_to_dec)
        print "the binary:%s ====>>>to dec:%s"%(tgtasc,bin_to_dec)
        print "the binary:%s ====>>>to ascii:%s"%(tgtasc,dec_to_asc)
    except Exception,e:
        print e

if __name__=='__main__':
    main()

运行截图:

windows平台运行,二进制不用加引号

 

posted @ 2018-08-29 15:32  Carrypan  阅读(3056)  评论(0编辑  收藏  举报