因为:
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))
所以:
# 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) -> 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