#-*- coding: utf-8
from decimal import Decimal
import os, sys
# global definition
# base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]
base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'), ord('A') + 6)]
# bin2dec
# 二进制 to 十进制: int(str,n=10)
def bin2dec(string_num):
return str(int(string_num, 2))
# hex2dec
# 十六进制 to 十进制
def hex2dec(string_num):
return str(int(string_num.upper(), 16))
# dec2bin
# 十进制 to 二进制: bin()
def dec2bin(string_num):
num = int(string_num)
mid = []
while True:
if num == 0: break
num, rem = divmod(num, 2)
mid.append(base[rem])
return ''.join([str(x) for x in mid[::-1]])
# dec2hex
# 十进制 to 八进制: oct()
# 十进制 to 十六进制: hex()
def dec2hex(string_num):
num = int(string_num)
mid = []
while True:
if num == 0: break
num, rem = divmod(num, 16)
mid.append(base[rem])
return ''.join([str(x) for x in mid[::-1]])
# hex2tobin
# 十六进制 to 二进制: bin(int(str,16))
def hex2bin(string_num):
return dec2bin(hex2dec(string_num.upper()))
# bin2hex
# 二进制 to 十六进制: hex(int(str,2))
def bin2hex(string_num):
return dec2hex(bin2dec(string_num))
def bTod(n, pre=4):
'''
把一个带小数的二进制数n转换成十进制
小数点后面保留pre位小数
'''
string_number1 = str(n) #number1 表示二进制数,number2表示十进制数
decimal = 0 #小数部分化成二进制后的值
flag = False
for i in string_number1: #判断是否含小数部分
if i == '.':
flag = True
break
if flag: #若二进制数含有小数部分
string_integer, string_decimal = string_number1.split('.') #分离整数部分和小数部分
for i in range(len(string_decimal)):
decimal += 2**(-i-1)*int(string_decimal[i]) #小数部分化成二进制
number2 = int(str(int(string_integer, 2))) + decimal
return round(number2, pre)
else: #若二进制数只有整数部分
return int(string_number1, 2)#若只有整数部分 直接一行代码二进制转十进制 python还是骚
def dTob(n, pre=4):
'''
把一个带小数的十进制数n转换成二进制
小数点后面保留pre位小数
'''
string_number1 = str(n) #number1 表示十进制数,number2表示二进制数
flag = False
for i in string_number1: #判断是否含小数部分
if i == '.':
flag = True
break
if flag:
string_integer, string_decimal = string_number1.split('.') #分离整数部分和小数部分
integer = int(string_integer)
decimal = Decimal(str(n)) - integer
l1 = [0,1]
l2 = []
decimal_convert = ""
while True:
if integer == 0: break
x,y = divmod(integer, 2) #x为商,y为余数
l2.append(y)
integer = x
string_integer = ''.join([str(j) for j in l2[::-1]]) #整数部分转换成二进制
print(len(string_integer))
i = 0
while decimal != 0 and i < pre:
result = int(decimal * 2)
decimal = decimal * 2 - result
decimal_convert = decimal_convert + str(result)
i = i + 1
string_number2 = string_integer + '.' + decimal_convert
exp_len = len(string_integer) - 1
return exp_len, string_number2
else: #若十进制只有整数部分
l1 = [0,1]
l2 = []
while True:
if n == 0: break
x,y = divmod(n, 2) #x为商,y为余数
l2.append(y)
n = x
string_number = ''.join([str(j) for j in l2[::-1]])
return int(string_number)
def flaot2memhex(n):
if n > 0:
flag = '0'
else:
flag = '1'
if __name__ == "__main__":
f = 12.5
exp_len, result = dTob(f, 8)
print(result)
print(len(result))
flag = '0'
int_part, dec_part = result.split('.')
print(int_part[1:1+exp_len])
data_part = int_part[1:1+exp_len] + dec_part
print(data_part)
memorybin = flag + str(bin(127+exp_len))[2:] + data_part + '0'*(23 - len(data_part))
print(memorybin)
result_hex= bin2hex(memorybin)
print(result_hex)