blob: 70affaca5c8d3f017784009463cc37508b186d40 [file] [log] [blame]
Nguyen Anh Quynh10983292014-05-17 09:51:15 +08001#!/usr/bin/env python
2# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
3
4from __future__ import print_function
5import sys
6_python3 = sys.version_info.major == 3
7
8
Nguyen Anh Quynhbbde6d52015-04-27 11:34:44 +08009def to_hex(s, prefix_0x = True):
Nguyen Anh Quynh10983292014-05-17 09:51:15 +080010 if _python3:
Nguyen Anh Quynhbbde6d52015-04-27 11:34:44 +080011 if prefix_0x:
12 return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK
13 else:
14 return " ".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK
Nguyen Anh Quynh10983292014-05-17 09:51:15 +080015 else:
Nguyen Anh Quynhbbde6d52015-04-27 11:34:44 +080016 if prefix_0x:
17 return " ".join("0x{0:02x}".format(ord(c)) for c in s)
18 else:
19 return " ".join("{0:02x}".format(ord(c)) for c in s)
Nguyen Anh Quynh10983292014-05-17 09:51:15 +080020
danghvu24120692014-09-27 17:59:42 -050021def to_hex2(s):
22 if _python3:
23 r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK
24 else:
25 r = "".join("{0:02x}".format(ord(c)) for c in s)
26 while r[0] == '0': r = r[1:]
27 return r
Nguyen Anh Quynh10983292014-05-17 09:51:15 +080028
29def to_x(s):
30 from struct import pack
31 if not s: return '0'
32 x = pack(">q", s)
33 while x[0] in ('\0', 0): x = x[1:]
danghvu24120692014-09-27 17:59:42 -050034 return to_hex2(x)
Nguyen Anh Quynh10983292014-05-17 09:51:15 +080035
36def to_x_32(s):
37 from struct import pack
38 if not s: return '0'
39 x = pack(">i", s)
40 while x[0] in ('\0', 0): x = x[1:]
danghvu24120692014-09-27 17:59:42 -050041 return to_hex2(x)