前言
不知道为什么,给脚本专栏选的这个logo有种怪怪的感觉(⊙o⊙)…
为方便后续一些脚本工作,将常用的进制转换函数汇总。所有函数均为字符串输入输出,且不加0x、0b等关键字,以便后续灵活调用。基于这些字符串输入输出的函数,可以非常灵活的根据需求拼接更加复杂的行为:
TO->>> |
十进制 |
二进制源码 |
二进制补码 |
十六进制 |
十进制 |
dec_to_bin |
dec_to_bnr |
dec_to_hex |
|
二进制源码 |
bin_to_dec |
bin_to_bnr |
bin_to_hex |
|
二进制补码 |
bnr_to_dec |
bnr_to_bin |
bnr_to_hex |
|
十六进制 |
hex_to_dec |
hex_to_bin |
hex_to_bnr |
|
FP32 |
float_to_hex |
函数
dec_to_bin
十进制转二进制源码:
def dec_to_bin(i: str) -> str: if not isinstance(i, str): raise TypeError("Not str input") return format(int(i),'b')#08b
测试:
print(dec_to_bin("10")) print(dec_to_bin("-10")) 1010 -1010
dec_to_bnr
十进制转二进制补码,我参考了网上的代码,稍作修改:
def dec_to_bnr(i: int, lenth: int = 1) -> str: if not isinstance(i, str): raise TypeError("Not str input") dec = int(i) digits = (len(bin(dec)) - 3 + 1) if dec < 0 else (len(bin(dec)) - 2) if digits >= lenth: lenth = digits pattern = f"{dec & int('0b' + '1' * lenth, 2):0{lenth}b}" return "".join(code for code in pattern)
测试:
print(dec_to_bnr("10")) print(dec_to_bnr("-10")) 1010 10110
dec_to_hex
十进制转十六进制:
def dec_to_hex(i: str) -> str: if not isinstance(i, str): raise TypeError("Not str input") if i.startswith("-"): i = re.sub("-", "", i) return "-" + str(hex(int(i)))[2:] else: return str(hex(int(i)))[2:]
测试:
print(dec_to_hex("10")) print(dec_to_hex("-10")) a -a
bin_to_dec
二进制转十进制:
def bin_to_dec(i: str) -> str: if not isinstance(i, str): raise TypeError("Not str input") return str(int(str(i), 2))
测试:
print(bin_to_dec("0101")) print(bin_to_dec("-0101")) 5 -5
bin_to_bnr
二进制源码转二进制补码:
def bin_to_bnr(i: str) -> str: return dec_to_bnr(bin_to_dec(i))
测试:
print(bin_to_bnr("1010")) print(bin_to_bnr("-1010")) 1010 10110
bin_to_hex
二进制原码转十六进制:
def bin_to_hex(i: str) -> str: if not isinstance(i, str): raise TypeError("Not str input") if i.startswith("-"): i = re.sub("-", "", i) return "-" + str(hex(int(i, 2)))[2:] else: return str(hex(int(i, 2)))[2:]
测试:
print(bin_to_hex("1010")) print(bin_to_hex("-1010")) a -a
bnr_to_dec
二进制补码转十进制,也是在参考了网上的代码:
def bnr_to_dec(i: str) -> str: if not isinstance(i, str): raise TypeError("Not str input") for num in i: if num not in ["0", "1"]: raise ValueError("Not bin str") if i.startswith("0"): dec = int(i, 2) else: dec = int(i[1:], 2) - 0x01 dec = -(~dec & int("0b" + "1" * (len(i) - 1), 2)) return str(dec)
测试:
print(bnr_to_dec("010011")) print(bnr_to_dec("1010011")) 19 -45
bnr_to_bin
二进制补码转二进制原码:
def bnr_to_bin(i: str) -> str: return dec_to_bin(bnr_to_dec(i))
bnr_to_hex
二进制补码转十六进制,这个场景必然是要把补码还原为原本的数,再显示十六进制,否则直接用bin_to_hex就够了:
def bnr_to_hex(i: str) -> str: return dec_to_hex(bnr_to_dec(i))
测试:
print(bnr_to_hex("10100")) print(bnr_to_hex("01001")) -c 9
hex_to_dec
十六进制转十进制:
def hex_to_dec(i: str) -> str: if not isinstance(i, str): raise TypeError("Not str input") return str(int(i, 16))
测试:
print(hex_to_dec("a")) print(hex_to_dec("-a")) 10 -10
hex_to_bin
十六进制转二进制:
def hex_to_bin(i: str) -> str: return dec_to_bin(hex_to_dec(i))
hex_to_bnr
十六进制转补码:
def hex_to_bnr(i: str) -> str: return dec_to_bnr(hex_to_dec(i))
float_to_hex
fp32(float)类型转十六进制,这个也是从网上学来的(感恩家人!):
import struct def float_to_hex(i: str) -> str: f = float(i) h = hex(struct.unpack('<I', struct.pack('<f', f))[0]) return str(h)[2:]
测试:
print(float_to_hex("17.5")) print(float_to_hex("-17.5")) 418c0000 c18c0000