我们的目标是┏ (゜ω゜)=☞芯片前端全栈工程师~喵!
前言
不知道为什么,给脚本专栏选的这个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
十进制转二进制源码:
1. def dec_to_bin(i: str) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. return format(int(i),'b')#08b
测试:
1. print(dec_to_bin("10")) 2. print(dec_to_bin("-10")) 3. 4. 1010 5. -1010
dec_to_bnr
十进制转二进制补码,我参考了网上的代码,稍作修改:
1. def dec_to_bnr(i: int, lenth: int = 1) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. dec = int(i) 5. digits = (len(bin(dec)) - 3 + 1) if dec < 0 else (len(bin(dec)) - 2) 6. if digits >= lenth: 7. lenth = digits 8. pattern = f"{dec & int('0b' + '1' * lenth, 2):0{lenth}b}" 9. return "".join(code for code in pattern)
测试:
1. print(dec_to_bnr("10")) 2. print(dec_to_bnr("-10")) 3. 4. 1010 5. 10110
dec_to_hex
十进制转十六进制:
1. def dec_to_hex(i: str) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. if i.startswith("-"): 5. i = re.sub("-", "", i) 6. return "-" + str(hex(int(i)))[2:] 7. else: 8. return str(hex(int(i)))[2:]
测试:
1. print(dec_to_hex("10")) 2. print(dec_to_hex("-10")) 3. 4. a 5. -a
bin_to_dec
二进制转十进制:
1. def bin_to_dec(i: str) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. return str(int(str(i), 2))
测试:
1. print(bin_to_dec("0101")) 2. print(bin_to_dec("-0101")) 3. 4. 5 5. -5
bin_to_bnr
二进制源码转二进制补码:
1. def bin_to_bnr(i: str) -> str: 2. return dec_to_bnr(bin_to_dec(i))
测试:
1. print(bin_to_bnr("1010")) 2. print(bin_to_bnr("-1010")) 3. 4. 1010 5. 10110
bin_to_hex
二进制原码转十六进制:
1. def bin_to_hex(i: str) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. if i.startswith("-"): 5. i = re.sub("-", "", i) 6. return "-" + str(hex(int(i, 2)))[2:] 7. else: 8. return str(hex(int(i, 2)))[2:]
测试:
1. print(bin_to_hex("1010")) 2. print(bin_to_hex("-1010")) 3. 4. a 5. -a
bnr_to_dec
二进制补码转十进制,也是在参考了网上的代码:
1. def bnr_to_dec(i: str) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. for num in i: 5. if num not in ["0", "1"]: 6. raise ValueError("Not bin str") 7. if i.startswith("0"): 8. dec = int(i, 2) 9. else: 10. dec = int(i[1:], 2) - 0x01 11. dec = -(~dec & int("0b" + "1" * (len(i) - 1), 2)) 12. return str(dec)
测试:
1. print(bnr_to_dec("010011")) 2. print(bnr_to_dec("1010011")) 3. 4. 19 5. -45
bnr_to_bin
二进制补码转二进制原码:
1. def bnr_to_bin(i: str) -> str: 2. return dec_to_bin(bnr_to_dec(i))
bnr_to_hex
二进制补码转十六进制,这个场景必然是要把补码还原为原本的数,再显示十六进制,否则直接用bin_to_hex就够了:
1. def bnr_to_hex(i: str) -> str: 2. return dec_to_hex(bnr_to_dec(i))
测试:
1. print(bnr_to_hex("10100")) 2. print(bnr_to_hex("01001")) 3. 4. -c 5. 9
hex_to_dec
十六进制转十进制:
1. def hex_to_dec(i: str) -> str: 2. if not isinstance(i, str): 3. raise TypeError("Not str input") 4. return str(int(i, 16))
测试:
1. print(hex_to_dec("a")) 2. print(hex_to_dec("-a")) 3. 4. 10 5. -10
hex_to_bin
十六进制转二进制:
1. def hex_to_bin(i: str) -> str: 2. return dec_to_bin(hex_to_dec(i))
hex_to_bnr
十六进制转补码:
1. def hex_to_bnr(i: str) -> str: 2. return dec_to_bnr(hex_to_dec(i))
float_to_hex
fp32(float)类型转十六进制,这个也是从网上学来的(感恩家人!):
1. import struct 2. def float_to_hex(i: str) -> str: 3. f = float(i) 4. h = hex(struct.unpack('<I', struct.pack('<f', f))[0]) 5. return str(h)[2:]
测试:
1. print(float_to_hex("17.5")) 2. print(float_to_hex("-17.5")) 3. 4. 418c0000 5. c18c0000