1from sys import version_info as pyVersion
2from binascii import hexlify, unhexlify
3
4
5if pyVersion.major == 3:
6 # py3 constants and conversion functions
7
8 stringTypes = (str,)
9 intTypes = (int, float)
10
11 def toString(string, encoding="utf-8"):
12 return string.decode(encoding)
13
14 def toBytes(string, encoding="utf-8"):
15 return string.encode(encoding)
16
17 def safeBinaryFromHex(hexadecimal):
18 if len(hexadecimal) % 2 == 1:
19 hexadecimal = "0" + hexadecimal
20 return unhexlify(hexadecimal)
21
22 def safeHexFromBinary(byteString):
23 return toString(hexlify(byteString))
24else:
25 # py2 constants and conversion functions
26
27 stringTypes = (str, unicode)
28 intTypes = (int, float, long)
29
30 def toString(string, encoding="utf-8"):
31 return string
32
33 def toBytes(string, encoding="utf-8"):
34 return string
35
36 def safeBinaryFromHex(hexadecimal):
37 return unhexlify(hexadecimal)
38
39 def safeHexFromBinary(byteString):
40 return hexlify(byteString)