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))
标签:numstr,name,python,lenght,fucontion,ssum,fstr,print From: https://www.cnblogs.com/geovindu/p/18262343