python: function and HexBinDecOct ,Convert binary, octal, decimal, and hexadecimal
# 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 io class BinaryConvert(object): """ 进制转换 """ def __init__(self): """ 实例化 """ self.numstr='' @staticmethod def 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] @staticmethod def 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 @staticmethod def DecimalConvert(numstr:str) -> int: """ 二进制字符串转十进制 字符串未倒过来 Octal Decimal Binary hexadecimal; sexadecimal :param numstr: 二进制字符 倒过来计算。从0开始索引 :return:整数 """ getstr = "" lenght = len(numstr) ssum=0 if lenght>0: if BinaryConvert.isBinary(numstr): index=0 for k in range(lenght-1,-1,-1 ): if numstr[k] == '1': ssum = ssum+1 * 2 ** index if numstr[k] == '0': ssum = ssum+ 0 index+=1 return ssum @staticmethod def OctalConvert(numstr:str) -> str: """ 二进制字符串转八进制 字符串未倒过来 :param numstr:二进制字符 :return: """ length=len(numstr) eight = "" if length>0: cc=0 if 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+=1 return eight[::-1] @staticmethod def HexadecimalConvert(numstr:str) -> str: """ 二进制字符串转十六进制 字符串未倒过来 :param numstr:进制字符串 :return: """ sixtee = "" length = len(numstr) cc = 0 if 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 += 1 return sixtee[::-1] @staticmethod def ReversedConvert(numstr:str) -> int: """ 二进制字符串转十进制 字符串倒过来 :param numstr: 二进制字符 :return: 整数 """ lenght = len(numstr) ssum = 0 if lenght > 0: if BinaryConvert.isBinary(numstr): for k in range(0,lenght): if numstr[k]=='1': ssum=ssum+1*2**k if numstr[k]=='0': ssum=ssum+0 return ssum @staticmethod def isBinary(numstr: str) -> bool: """ 判断是否二进制字符串,必须包含为1或0 :param numstr:二进制字符串 :return: 是否为真 """ length = len(numstr) isok = False if length > 0: for k in range(0, len(numstr)): if numstr[k] == '0' or numstr[k] == '1': isok = True else: isok = False break return isok @staticmethod def 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 value size = len(hexadecimalstr) - 1 if size>0: for num in hexadecimalstr: reInt = reInt + table[num] * 16 ** size size = size - 1 print("十六进制转十进制", reInt) return reInt @staticmethod def hexadecimalConvertTen(hexadecimalstr: str) -> int: """ 十六制转十进制 :param hexadecimalstr: 十六进制字符串 :return: """ #hex = '3B' # input("Enter Hexadecimal Number: ") c = count = i = 0 lenght = len(hexadecimalstr) - 1 while 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]) - 55 elif hexadecimalstr[lenght] >= 'a' and hexadecimalstr[lenght] <= 'f': rem = ord(hexadecimalstr[lenght]) - 87 else: c = 1 break count = count + (rem * (16 ** i)) lenght = lenght - 1 i = i + 1 return count @staticmethod def 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 @staticmethod def DecimalConvertBin(numint:int) -> str: """ 十进制转二进制字符串 :param numint: :return: """ #numint = 59 numstr = [] binstring = '' while numint > 0: # 余数进栈 renum = numint % 2 numstr.append(renum ) numint = numint // 2 while len(numstr) > 0: # 元素全部出栈即为所求二进制数 binstring = binstring + str(numstr.pop()) return binstring @staticmethod def DecimalConvertOct(numint: int) -> str: """ 十进制转八进制 :param numint: :return: """ #numint = 59 numstr = [] octtring = '' while numint > 0: # 余数进栈 renum = numint % 8 numstr.append(renum ) numint = numint // 8 while len(numstr) > 0: # 元素全部出栈即为所求八进制数 octtring = octtring + str(numstr.pop()) return octtring @staticmethod def DecimalConvertHex(numint: int) -> str: """ 十进制转十六进制 :param numint: :return: """ #numint = 59 numstr = [] hexstring = '' while numint > 0: # 余数进栈 renum = numint % 16 numstr.append(BinaryConvert.hexFoo(renum)) numint = numint // 16 while len(numstr) > 0: # 元素全部出栈即为所求十六进制数 hexstring = hexstring + str(numstr.pop()) return hexstring
def hexFoo(k): """ 十六進制 :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 def DecimalConvert(numstr) -> int: """ 十进制 字符串未倒过来 Octal Decimal Binary hexadecimal; sexadecimal :param numstr: 二进制字符 倒过来计算。从0开始索引 :return:整数 """ getstr = "" lenght = len(numstr) ssum=0 if(lenght>0): if isBinary(numstr): index=0 for k in range(lenght-1,-1,-1 ): if (numstr[k] == '1'): ssum = ssum+1 * 2 ** index if (numstr[k] == '0'): ssum = ssum+ 0 index+=1 return ssum def OctalConvert(numstr:str) -> str: """ 八进制 字符串未倒过来 :param numstr:二进制字符 :return: """ length=len(numstr) eight = "" if(length>0): cc=0 if isBinary(numstr): for k in range(length, -1, -3): if (k >= 3): #print(k, DecimalCovert(numstr[k - 3:k])) eight = eight + str(DecimalConvert(numstr[k - 3:k])) if (0<k < 3): #print(DecimalCovert(numstr[:k])) eight = eight + str(DecimalConvert(numstr[:k])) cc+=1 return eight[::-1] def HexadecimalConvert(numstr:str) -> str: """ 十六进制 字符串未倒过来 :param numstr: :return: """ sixtee = "" length = len(numstr) cc = 0 if(length>0): if isBinary(numstr): for k in range(length, -1, -4): # 1 次处理一位 # print(k, sixteenFoo(threeCovert(numstr[k - 4:k]))) if (k >= 4): sixtee = sixtee + hexFoo(DecimalConvert(numstr[k - 4:k])) if (0<k < 4): #print(hexFoo(DecimalConvert(numstr[:k]))) sixtee = sixtee + hexFoo(DecimalConvert(numstr[:k])) cc += 1 return sixtee[::-1] def isBinary(numstr:str) -> bool: """ 判断是否二进制字符串,必须包含为1或0 :param numstr: :return: """ length=len(numstr) isok = False if(length>0): for k in range(0, len(numstr)): if (numstr[k] == '0' or numstr[k] == '1'): isok = True else: isok = False break return isok def ReversedConvert(numstr:str) -> int: """ 十进制 字符串倒过来 :param numstr: 二进制字符 :return: 整数 """ lenght = len(numstr) ssum = 0 if (lenght > 0): for k in range(0,lenght): if(numstr[k]=='1'): ssum=ssum+1*2**k if(numstr[k]=='0'): ssum=ssum+0 return ssum # Press the green button in the gutter to run the script. if __name__ == '__main__': #print_hi('PyCharm,python language') numstr = '111011' isok=False for k in range(0,len(numstr)): if(numstr[k]=='0' or numstr[k]=='1'): isok=True else: isok=False break print(isok) print(isBinary(numstr)) print(OctalConvert(numstr)) print(HexadecimalConvert(numstr)) print(DecimalConvert(numstr)) length = len(numstr) print(length, length // 3, length % 3) print(length, length // 4, length % 4) print('************') eight = "" for k in range(length, -1, -3): if (k >= 3): eight = eight + str(DecimalConvert(numstr[k - 3:k])) if (0<k < 3): eight = eight + str(DecimalConvert(numstr[:k])) print(eight[::-1]) print('************') sixtee = "" cc = 0 for k in range(length, -1, -4): # 1 次处理一位 if (k >= 4): sixtee = sixtee + hexFoo(DecimalConvert(numstr[k - 4:k])) if (0<k < 4): sixtee = sixtee + hexFoo(DecimalConvert(numstr[:k])) cc += 1 print(sixtee[::-1], cc) print('************') num=0b111011 print(num) num = '111011' print(DecimalConvert(num)) print(OctalConvert(num)) num = num[::-1] sm=ReversedConvert(num) print(sm) print(num) num=''.join(reversed(num)) print(num)
#十进制转二进制,八进制,十六进制 numint=59 numstr=[] binstring = '' while numint > 0: # 余数进栈 rem = numint % 2 numstr.append(rem) numint = numint // 2 while len(numstr) > 0: # 元素全部出栈即为所求二进制数 binstring = binstring + str(numstr.pop()) print("二进制:"+binstring) print('************') numint = 59 numstr=[] octtring = '' while numint > 0: # 余数进栈 rem = numint % 8 numstr.append(rem) numint = numint // 8 while len(numstr) > 0: # 元素全部出栈即为所求八进制数 octtring = octtring + str(numstr.pop()) print("八进制:"+octtring) print('************') numint = 59 numstr=[] hexstring = '' while numint > 0: # 余数进栈 rem = numint % 16 numstr.append(hexFoo(rem)) numint = numint // 16 while len(numstr) > 0: # 元素全部出栈即为所求十六进制数 hexstring = hexstring + str(numstr.pop()) print("十六进制:"+hexstring)
#十六进制转十进制 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() res = 0 # computing max power value size = len(hexadecimal) - 1 for num in hexadecimal: res = res + table[num] * 16 ** size size = size - 1 print("十六进制转十进制",res) hex ='3B' #input("Enter Hexadecimal Number: ") c = count = i = 0 lenght = len(hex) - 1 while lenght >= 0: if hex[lenght] >= '0' and hex[lenght] <= '9': rem = int(hex[lenght]) elif hex[lenght] >= 'A' and hex[lenght] <= 'F': rem = ord(hex[lenght]) - 55 elif hex[lenght] >= 'a' and hex[lenght] <= 'f': rem = ord(hex[lenght]) - 87 else: c = 1 break count = count + (rem * (16 ** i)) lenght = lenght - 1 i = i + 1 rem=None print("十六进制转十进制",count) print('************')
def sixteenFoo(k): """ 十六進制 :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 def threeCovert(numstr) -> int: """ :param numstr: :return: """ lenght = len(numstr) ssum = 0 if (lenght > 0): index = 0 for k in range(lenght - 1, -1, -1): if (numstr[k] == '1'): ssum = ssum + 1 * 2 ** index if (numstr[k] == '0'): ssum = ssum + 0 index += 1 return ssum def tenCovert(numstr) -> int: """ 十进制 :param numstr: 二进制字符 :return:整数 """ getstr = "" lenght = len(numstr) ssum=0 if(lenght>0): index=0 for k in range(lenght-1,-1,-1 ): if (numstr[k] == '1'): ssum = ssum+1 * 2 ** index if (numstr[k] == '0'): ssum = ssum+ 0 index+=1 return ssum def sixCovert(numstr: str): """ :param numstr: 二进制字符 :return: """ getstr="" lenght=len(numstr) if(lenght>0): cshu=0 for k in range(0,lenght,4): cshu+=1 return getstr def print_hi(name): # Use a breakpoint in the code line below to debug your script. print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. # Press the green button in the gutter to run the script. if __name__ == '__main__': print_hi('PyCharm') numstr=f'1001110' print(tenCovert(numstr)) length=len(numstr) print(length,length//3,length%3) print(length,length // 4, length % 4) print('************') eight="" for k in range(length,-1,-3): if(k>=3): print(k,threeCovert(numstr[k-3:k])) eight =eight+str(threeCovert(numstr[k-3:k])) if(k<3): print(threeCovert(numstr[:k])) eight = eight + str(threeCovert(numstr[:k])) print(eight) print('************') sixtee="" cc=0 for k in range(length,-1,-4): # 1 次处理一位 #print(k, sixteenFoo(threeCovert(numstr[k - 4:k]))) if(k>=4): sixtee =sixtee+sixteenFoo(threeCovert(numstr[k - 4:k])) if (k < 4): print(sixteenFoo(threeCovert(numstr[:k]))) sixtee=sixtee+sixteenFoo(threeCovert(numstr[:k])) cc+=1 print(sixtee,cc) print('************') print(sixteenFoo(4)) name=input("please name:") b=f"你好{name},歡迎光臨!" print(b) b = "你好%s,歡迎光臨!"%name print(b) b = "你好"+name+",歡迎光臨!" print(b) b = "你好{0}".format(name)+"歡迎光臨!" print(b) f=0b1001110 # 十进制 int() a=0*2**0+1*2**1+1*2**2+1*2**3+0*2**4+0*2**5+1*2**6 print(a) # 八进制 oct() print(78/8,78%8) # 110 001 001 8 116 print(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 4E print(sixteenFoo(0*2**0+0*2**1+1*2**2),sixteenFoo(0*2**0+1*2**1+1*2**2+1*2**3))
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)