【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平台運行,二進制不用加引號

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM