-
Notifications
You must be signed in to change notification settings - Fork 1
/
hexHelper.py
60 lines (49 loc) · 1018 Bytes
/
hexHelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 进制转换工具
def d2x(d):
"""
:param d: int
:return: str
"""
return hex(d)
def d2x8(d):
"""
:param d: int
:return: str(len = 8)
"""
x = d2x(d)[2:]
if len(x) < 8:
x = "0" * (8 - len(x)) + x
return "0x" + x
def d2xK(d, k):
"""
:param d: int
:param k: int
:return: str(len = k)
"""
x = d2x(d)[2:]
if len(x) < k:
x = "0" * (k - len(x)) + x
return "0x" + x
def d2b(d):
"""
:param d: int
:return: str
"""
return bin(d)
def d2bK(d, k):
"""
:param d: int
:param k: int
:return: str(len = k)
"""
b = d2b(d)[2:]
if len(b) < k:
b = "0" * (k - len(b)) + b
return "0b" + b
if __name__ == '__main__':
binary = d2bK(255, 16)
print("255 =", binary, type(binary))
hexadecimal = d2xK(65535, 8)
print("65535 =", hexadecimal, type(hexadecimal))