Python3 进制转换(二进制 十进制 十六进制 ASCII字符串转换)

Python3 进制转换(二进制 十进制 十六进制 ASCII字符串转换)

Python3 进制转换

编写Convertor类,用于进制与ASCII字符之间互相转换,便于使用

ASCII码及进制之间转换关系

ASCII码介绍:

ASCII 全称为 ( American Standard Code for Information Interchange),简单的说,就是用 7 位二进制 ( 即 十进制表示为 0 到 127 ) (即 十六进制0x00–0x7F) 去编码我们生活中常见的数字,大小写字母,标点符号以及一些特殊的控制字符,如下:

  • 数字:0 , 1 , 2 , 3 … 9
  • 字母:a , b , c …z , A , B , C … Z
  • 标点符号以及运算符:,. + - …
  • 控制字符:回车,换行等控制字符

Convertor类的使用:

#!/usr/local/bin/env python3
import struct, sys

base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'), ord('A') + 6)]

'''
    int(x [,base ])         将x转换为一个整数
    long(x [,base ])        将x转换为一个长整数
    float(x )               将x转换到一个浮点数
    complex(real [,imag ])  创建一个复数
    str(x )                 将对象 x 转换为字符串
    repr(x )                将对象 x 转换为表达式字符串
    eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象
    tuple(s )               将序列 s 转换为一个元组
    list(s )                将序列 s 转换为一个列表
    chr(x )                 将一个整数转换为一个字符
    unichr(x )              将一个整数转换为Unicode字符
    ord(x )                 将一个字符转换为它的整数值
    hex(x )                 将一个整数转换为一个十六进制字符串
    oct(x )                 将一个整数转换为一个八进制字符串
'''

class Convertor(object):

    def __init__(self):
        self.base = base
        # self.base = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        print("Convertor instance initialized")

    # Todo: binary to decimal
    # eg: bin2dec("00110010") => 50
    def bin2dec(self, string_num):
        return int(string_num, 2)

    # Todo: binary to hex
    # eg: bin2hex("1110") => E
    def bin2hex(self, string_num):
        return self.dec2hex(self.bin2dec(string_num))

    # Todo: decimal to binary
    # eg: dec2bin(2) => 10
    def dec2bin(self, string_num):
        num = int(string_num)
        mid = []
        while True:
            if num == 0: break
            num, rem = divmod(num, 2)
            mid.append(self.base[rem])
        return ''.join([str(x) for x in mid[::-1]])

    # Todo: decimal to hex
    # eg: dec2hex(100) => 64
    def dec2hex(self, string_num):
        num = int(string_num)
        mid = []
        while True:
            if num == 0: break
            num, rem = divmod(num, 16)
            mid.append(self.base[rem])

        return ''.join([str(x) for x in mid[::-1]])

    # Todo: hex to binary
    # eg: hex2bin("DF215678ff") => 1101111100100001010101100111100011111111
    def hex2bin(self, string_num):
        return self.dec2bin(self.hex2dec(string_num.upper()))

    # Todo: hex to decimal
    # eg: hex2dec("FF") => 255
    def hex2dec(szelf, string_num):
        return int(string_num.upper(), 16)

    # Todo: hex(00--7F) to ASCII
    # eg: hex2ASCII("56 65 76 44") => VevD
    #     hex2ASCII("56657644") => VevD
    def hex2ASCII(self, string_hex, littleEndian=False):
        hexStr = (string_hex.strip()).replace(" ", "")
        if littleEndian == True:
            hexStr = self.littleEndian(hexStr)
        asciiVal = bytes.fromhex(hexStr).decode("ASCII")
        return asciiVal

    # Todo: hex(00--FF) to float
    # eg: hex2float("40 36 2A A3") => 2.846352338791
    #     hex2float("A3 2A 36 40", littleEndian=True) => 2.846352338791
    def hex2float(self,target_num, littleEndian=False):
        target_num = target_num.replace(" ", "")
        if littleEndian == True:
            if target_num.startswith("0x"):
                target_num = "0x{}".format(self.littleEndian(target_num[2::]))
            else:
                target_num = "0x" + self.littleEndian(target_num)
        try:
            if "0x" in target_num:
                target_num = target_num.split("0x")[1]
            if " " in target_num:
                target_num = target_num.replace(" ", "")
            hex1 = bytes.fromhex(target_num)
            res = struct.unpack("!f", hex1)[0]
        except Exception as e:
            print("\033[1;31m{}\033[0m".format(e))
            res = 0
        return float(res)

    # Todo: little endian for hex string value
    # eg:   littleEndian("56 77 43") => "437756"
    def littleEndian(self, hexStr):
        if len(hexStr) %2 !=0:
            raise Exception("non-hexadecimal number found in \"{}\"".format(hexStr))
        littleEndianHex = ""
        for i in range(len(hexStr) - 1, -1, -1):
            if i % 2 == 0:
                littleEndianHex = "{}{}{}".format(littleEndianHex, hexStr[i], hexStr[i + 1])
        hexStr = littleEndianHex
        return hexStr

使用说明:

  1. 二进制转换:
    bin2dec(bin_val)及bin2hex(binary)转换参数为字符串如: “0100”/“10”/“10001100”
  2. 十进制转换:
    dec2bin(decimal)及dec2hex(decimal)参数decimal类型需要为整数;
  3. 十六进制转换:
    hex2bin(hexVar)参数需要为十六进制字符,不允许长度为单数,eg:“67DFC2”;
    hex2dec(hexVar,littleEndian=False), 参数字符串hexVar在方法内会先进行trim空行空格和删除空格的动作,对处理过的字符进行转换,littleEndian如为True,会对字符串进行littleEndian动作,如"47657F" => “7F6547”;
    hex2ASCII(string_hex, littleEndian=False)将十六进制字符串**(00–7F)**转换成ASCII字符串; 十六进制字符80–FF是non-ASCII字符,不能转换成ASCII字符
    hex2float(target_num, littleEndian=False)将十六进制字符转换为float
    littleEndian(hexStr)可以将十六进制字符串进行倒置"57697F" => “7F6957”

使用案例:

主程式:

#!/usr/local/bin/env python3

from Convertor import Convertor

if __name__ == "__main__":
    convertor = Convertor()

    # binary to decimal
    bin_val = "00110010"
    decimal = convertor.bin2dec(bin_val)
    print("bin2dec:%d"%decimal)

    # binary to hex
    binary = "1110"
    hexVal = convertor.bin2hex(binary)
    print("bin2hex:%s"%hexVal)

    # decimal to bin
    decimal = 2
    binVal  = convertor.dec2bin(decimal)
    print("dec2bin:%s"%binVal)

    # decimal to hex
    decimal = 100
    hexVal = convertor.dec2hex(decimal)
    print("dec2hex:%s"%hexVal)

    # hex to binary
    hexVar = "DF215678ff"
    binVal = convertor.hex2bin(hexVar)
    print("hex2bin:%s"%binVal)

    # hex to decimal
    hexVar = "FF"
    decimalVal = convertor.hex2dec(hexVar)
    print("hex2dec:%d"%decimalVal)

    # hex string to ASCII
    hexStr = "56 65 76 44"
    asciiVal = convertor.hex2ASCII(hexStr)
    asciiLittleEndianVal = convertor.hex2ASCII(hexStr,littleEndian=True)
    print("hex2ASCII:%s\nasciiLittleEndianVal:%s"%(asciiVal,asciiLittleEndianVal))

    # hex to float
    hexStr = "40 36 2A A3"
    floatVal = convertor.hex2float(hexStr)
    print("float value:%.12f"%floatVal)

输出:

Convertor instance initialized
bin2dec:50
bin2hex:E
dec2bin:10
dec2hex:64
hex2bin:1101111100100001010101100111100011111111
hex2dec:255
hex2ASCII:VevD
asciiLittleEndianVal:DveV
float value:2.846352338791

通过外部函数名字符串,调用Convertor类的对应方法进行进制转换

代码:

if __name__ == "__main__":
    convertor = Convertor()
    functionPointer = getattr(convertor, sys.argv[1])
    retVal = None
    if len(sys.argv) >= 4 and sys.argv[3] == "littleEndian":
        try:
            retVal = functionPointer(sys.argv[2], littleEndian=True)
        except Exception as e:
            print("\033[1;31m{}\033[0m".format(e))
            retVal = functionPointer(sys.argv[2])
    else:
        retVal = functionPointer(sys.argv[2])
    print(retVal)

命令行参数执行:

python3 Convertor.py hex2bin "7F7F"

在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页