1from base64 import b64encode, b64decode
2from .compatibility import safeHexFromBinary, safeBinaryFromHex, toString
3
4
5def hexFromInt(number):
6 hexadecimal = "{0:x}".format(number)
7 if len(hexadecimal) % 2 == 1:
8 hexadecimal = "0" + hexadecimal
9 return hexadecimal
10
11
12def intFromHex(hexadecimal):
13 return int(hexadecimal, 16)
14
15
16def hexFromByteString(byteString):
17 return safeHexFromBinary(byteString)
18
19
20def byteStringFromHex(hexadecimal):
21 return safeBinaryFromHex(hexadecimal)
22
23
24def numberFromByteString(byteString):
25 return intFromHex(hexFromByteString(byteString))
26
27
28def base64FromByteString(byteString):
29 return toString(b64encode(byteString))
30
31
32def byteStringFromBase64(base64String):
33 return b64decode(base64String)
34
35
36def bitsFromHex(hexadecimal):
37 return format(intFromHex(hexadecimal), 'b').zfill(4 * len(hexadecimal))