因为:
f=0b1001110# 十进制 int()a=0*2**0+1*2**1+1*2**2+1*2**3+0*2**4+0*2**5+1*2**6print(a)# 八进制 oct()print(78/8,78%8)# 110 001 001 8 116print(1*2**0+0*2**1+0*2**2,1*2**0+0*2**1+0*2**2,0*2**0+1*2**1+1*2**2)#十六进制 hex()#0 100 1110 16 4Eprint(sixteenFoo(0*2**0+0*2**1+1*2**2),sixteenFoo(0*2**0+1*2**1+1*2**2+1*2**3))
所以:
# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2023.1 python 3.11
# Datetime : 2024/6/22 20:01
# User : geovindu
# Product : PyCharm
# Project : pyBaiduAi
# File : BinaryConvert.py
# explain : 学习
import sys
import os
import ioclass BinaryConvert(object):"""进制转换"""def __init__(self):"""实例化"""self.numstr=''@staticmethoddef hexFoo(k: int) -> str:"""整数十六進制:param k::return:"""# hexStr=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']hexStr = '0123456789ABCDEF'return hexStr[k]@staticmethoddef hexFoo2(k: int) -> str:"""整数转十六進制:param k: 整数:return:"""fstr = ''if k == 10:fstr = "A"elif k == 11:fstr = "B"elif k == 12:fstr = "C"elif k == 13:fstr = "D"elif k == 14:fstr = "E"elif k == 15:fstr = "F"else:fstr = str(k)return fstr@staticmethoddef DecimalConvert(numstr) -> int:"""二进制字符串转十进制 字符串未倒过来 Octal Decimal Binary hexadecimal; sexadecimal:param numstr: 二进制字符 倒过来计算。从0开始索引:return:整数"""getstr = ""lenght = len(numstr)ssum=0if lenght>0:if BinaryConvert.isBinary(numstr):index=0for k in range(lenght-1,-1,-1 ):if numstr[k] == '1':ssum = ssum+1 * 2 ** indexif numstr[k] == '0':ssum = ssum+ 0index+=1return ssum@staticmethoddef OctalConvert(numstr:str) -> str:"""二进制字符串转八进制 字符串未倒过来:param numstr:二进制字符:return:"""length=len(numstr)eight = ""if length>0:cc=0if BinaryConvert.isBinary(numstr):for k in range(length, -1, -3):if k >= 3:#print(k, DecimalCovert(numstr[k - 3:k]))eight = eight + str(BinaryConvert.DecimalConvert(numstr[k - 3:k]))if 0<k < 3:#print(DecimalCovert(numstr[:k]))eight = eight + str(BinaryConvert.DecimalConvert(numstr[:k]))cc+=1return eight[::-1]@staticmethoddef HexadecimalConvert(numstr:str) -> str:"""二进制字符串转十六进制 字符串未倒过来:param numstr:进制字符串:return:"""sixtee = ""length = len(numstr)cc = 0if length>0:if BinaryConvert.isBinary(numstr):for k in range(length, -1, -4): # 1 次处理一位# print(k, sixteenFoo(threeCovert(numstr[k - 4:k])))if k >= 4:sixtee = sixtee + BinaryConvert.hexFoo(BinaryConvert.DecimalConvert(numstr[k - 4:k]))if 0<k < 4:#print(hexFoo(DecimalConvert(numstr[:k])))sixtee = sixtee + BinaryConvert.hexFoo(BinaryConvert.DecimalConvert(numstr[:k]))cc += 1return sixtee[::-1]@staticmethoddef ReversedConvert(numstr:str) -> int:"""二进制字符串转十进制 字符串倒过来:param numstr: 二进制字符:return: 整数"""lenght = len(numstr)ssum = 0if lenght > 0:if BinaryConvert.isBinary(numstr):for k in range(0,lenght):if numstr[k]=='1':ssum=ssum+1*2**kif numstr[k]=='0':ssum=ssum+0return ssum@staticmethoddef isBinary(numstr: str) -> bool:"""判断是否二进制字符串,必须包含为1或0:param numstr:二进制字符串:return: 是否为真"""length = len(numstr)isok = Falseif length > 0:for k in range(0, len(numstr)):if numstr[k] == '0' or numstr[k] == '1':isok = Trueelse:isok = Falsebreakreturn isok@staticmethoddef SixConvertTen(hexadecimalstr:str) -> int:"""十六制转十进制:param hexadecimalstr: 十六进制字符串:return: 返回整数"""table = {'0': 0, '1': 1, '2': 2, '3': 3,'4': 4, '5': 5, '6': 6, '7': 7,'8': 8, '9': 9, 'A': 10, 'B': 11,'C': 12, 'D': 13, 'E': 14, 'F': 15}#hexadecimal = '3B' # input("Enter Hexadecimal Number: ").strip().upper()reInt = 0# computing max power valuesize = len(hexadecimalstr) - 1if size>0:for num in hexadecimalstr:reInt = reInt + table[num] * 16 ** sizesize = size - 1print("十六进制转十进制", reInt)return reInt@staticmethoddef hexadecimalConvertTen(hexadecimalstr: str) -> int:"""十六制转十进制:param hexadecimalstr: 十六进制字符串:return:"""#hex = '3B' # input("Enter Hexadecimal Number: ")c = count = i = 0lenght = len(hexadecimalstr) - 1while lenght >= 0:if hexadecimalstr[lenght] >= '0' and hexadecimalstr[lenght] <= '9':rem = int(hexadecimalstr[lenght])elif hexadecimalstr[lenght] >= 'A' and hexadecimalstr[lenght] <= 'F':rem = ord(hexadecimalstr[lenght]) - 55elif hexadecimalstr[lenght] >= 'a' and hexadecimalstr[lenght] <= 'f':rem = ord(hexadecimalstr[lenght]) - 87else:c = 1breakcount = count + (rem * (16 ** i))lenght = lenght - 1i = i + 1return count@staticmethoddef hexadecimalConvertBin(hexadecimalstr: str) -> str:"""十六制转二进制:param hexadecimalstr::return:"""hexDict = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110','7': '0111','8': '1000', '9': '1001', 'a': '1010', 'b': '1011', 'c': '1100', 'd': '1101', 'e': '1110','f': '1111'}binary = ''if len(hexadecimalstr)>0:hexadecimalstr = hexadecimalstr.lower() # '3b'for digit in hexadecimalstr:binary += hexDict[digit]print(binary)return binary@staticmethoddef DecimalConvertBin(numint:int) -> str:"""十进制转二进制字符串:param numint::return:"""#numint = 59numstr = []binstring = ''while numint > 0: renum = numint % 2numstr.append(renum )numint = numint // 2while len(numstr) > 0: binstring = binstring + str(numstr.pop())return binstring@staticmethoddef DecimalConvertOct(numint: int) -> str:"""十进制转八进制:param numint::return:"""#numint = 59numstr = []octtring = ''while numint > 0: renum = numint % 8numstr.append(renum )numint = numint // 8while len(numstr) > 0: octtring = octtring + str(numstr.pop())return octtring@staticmethoddef DecimalConvertHex(numint: int) -> str:"""十进制转十六进制:param numint::return:"""#numint = 59numstr = []hexstring = ''while numint > 0: renum = numint % 16numstr.append(BinaryConvert.hexFoo(renum))numint = numint // 16while len(numstr) > 0: hexstring = hexstring + str(numstr.pop())return hexstring