1. 程式人生 > >不同進位制數字轉換

不同進位制數字轉換

1、十進位制轉換為其他進位制

#將十進位制數轉換為二進位制
def dec2bin(dec):
    if checkdec(dec):
        return str(bin(int(dec)))
#將十進位制數轉換為八進位制
def dec2oct(dec):
    if checkdec(dec):
        return str(oct(int(dec)))

#將十進位制數轉換為十六進位制
def dec2hex(dec):
    if checkdec(dec):
        return str(hex(int(dec)))
#校驗十進位制數字
def checkdec(dec): matchobj = re.match(r'(0[dD])?[0-9]+$',dec) if matchobj: return True else: return False

測試用例

class DecMethodsTest(unittest.TestCase):
    def test_dec2bin(self):
        self.assertEqual(IPNumConvertMethods.dec2bin('10'),'0b1010')

    def test_dec2oct(
self): self.assertEqual(IPNumConvertMethods.dec2oct('10'),'0o12') def test_dec2hex(self): self.assertEqual(IPNumConvertMethods.dec2hex('10'), '0xa')

2、二進位制轉換為其他進位制

#將二進位制數轉換為十進位制
def bin2dec(bin):
    if checkbin(bin):
        return str(int(bin, 2))
    else:
        return ""
#將二進位制數轉換為八進位制 def bin2oct(bin): if checkbin(bin): return str(oct(int(bin, 2))) else: return "" #將二進位制數轉換為十六進位制 def bin2hex(bin): if checkbin(bin): return str(hex(int(bin, 2))) else: return "" #校驗二進位制數字 def checkbin(bin): matchobj = re.match(r'(0[bB])?[0-1]+$',bin) if matchobj: return True else: return False

測試用例

class BinMethodsTest(unittest.TestCase):
    def test_bin2dec(self):
        self.assertEqual(IPNumConvertMethods.bin2dec('1010'),'10')

    def test_bin2oct(self):
        self.assertEqual(IPNumConvertMethods.bin2oct('1010'), '0o12')

    def test_bin2hex(self):
        self.assertEqual(IPNumConvertMethods.bin2hex('1010'), '0xa')

3、八進位制轉換為其他進位制

#將八進位制數轉換為十進位制
def oct2dec(oct):
    if checkoct(oct):
        return str(int(oct,8))
    else:
        return ""
#將八進位制數轉換為二進位制
def oct2bin(oct):
    if checkoct(oct):
        return str(bin(int(oct,8)))
    else:
        return ""
#將八進位制數轉換為十六進位制
def oct2hex(oct):
    if checkoct(oct):
        return str(hex(int(oct,8)))
    else:
        return ""
#校驗八進位制數字
def checkoct(oct):
    matchobj = re.match(r'(0[oO])?[0-7]+$',oct)
    if matchobj:
        return True
    else:
        return False

測試用例

class OctMethodsTest(unittest.TestCase):
    def test_oct2bin(self):
        self.assertEqual(IPNumConvertMethods.oct2bin('12'),'0b1010')

    def test_oct2dec(self):
        self.assertEqual(IPNumConvertMethods.oct2dec('12'),'10')

    def test_oct2hex(self):
        self.assertEqual(IPNumConvertMethods.oct2hex('0o12'),'0xa')

4、十六進位制轉換為其他進位制

#將十六進位制數轉換為十進位制
def hex2dec(hex):
    if checkhex(hex):
        return str(int(hex,16))
    else:
        return ""
#將十六進位制數轉換為二進位制
def hex2bin(hex):
    if checkhex(hex):
        return str(bin(int(hex,16)))
    else:
        return ""
#將十六進位制數轉換為八進位制
def hex2oct(hex):
    if checkhex(hex):
        return str(oct(int(hex,16)))
    else:
        return ""
       
#校驗十六進位制
def checkhex(hex):
    matchobj = re.match(r'(0[xX])?[0-9a-fA-F]+$',hex)
    if matchobj:
        return True
    else:
        return False

測試用例

class HexMethodsTest(unittest.TestCase):

    def test_hex2bin(self):
        self.assertEqual(IPNumConvertMethods.hex2bin('0xa'),'0b1010')

    def test_hex2dec(self):
        self.assertEqual(IPNumConvertMethods.hex2dec('0xa'),'10')

    def test_hex2hex(self):
        self.assertEqual(IPNumConvertMethods.hex2oct('0xa'),'0o12')