进制转换脚本

Home / Blog / Download / About me

进制转换脚本

Directory

最近研究数据库底层字节存储,经常用到进制转换。把python的稍微写成脚本了,效果如下:

<li@whf307/Users/li/Desktop >./bodh.py
chose one u want 2 exchange
1  ------->>> 2 to 10
2  ------->>> 2 to 16
3  ------->>> 10 to 2
4  ------->>> 10 to 16
5  ------->>> 16 to 2
6  ------->>> 16 to 10
6
0a
###########################
so it's  10
###########################
chose one u want 2 exchange
1  ------->>> 2 to 10
2  ------->>> 2 to 16
3  ------->>> 10 to 2
4  ------->>> 10 to 16
5  ------->>> 16 to 2
6  ------->>> 16 to 10
6
0d
###########################
so it's  13
###########################

脚本如下:

#!/usr/bin/python
###################################################################################################################################
## Author      : whf307
## Created     : 2019-01-18
## Mail        : whf307@gmail.com
## Website     : https://whf307.github.io
## For what    : exchange bin,hex,dec
###################################################################################################################################

import sys
while True:
        try:
                print 'chose one u want 2 exchange'
                a = raw_input('1  ------->>> 2 to 10\n2  ------->>> 2 to 16\n3  ------->>> 10 to 2\n4  ------->>> 10 to 16\n5  ------->>> 16 to 2\n6  ------->>> 16 to 10\n')
                if a == '1':
                        b = raw_input()
                        print '###########################'
                        print 'so it\'s  '  + str(int(b,2))
                        print '###########################' + '\n'
                elif a == '2':
                        b = raw_input()
                        print '###########################'
                        print 'so it\'s  '  + str(hex(int(b,2)))
                        print '###########################' + '\n'
                elif a == '3':
                        b = int(raw_input())
                        print '###########################'
                        print 'so it\'s  '  + str(bin(b))
                        print '###########################' + '\n'
                elif a == '4':
                        b = int(raw_input())
                        print '###########################'
                        print 'so it\'s  '  + hex(b)
                        print '###########################' + '\n'
                elif a == '5':
                        b = raw_input()
                        print '###########################'
                        print 'so it\'s  '  + bin(int(b,16))
                        print '###########################' + '\n'
                elif a == '6':
                        b = raw_input()
                        print '###########################'
                        print 'so it\'s  '  + str(int(b,16))
                        print '###########################'
        except EOFError:
                sys.exit()