Nguyen Anh Quynh | 1098329 | 2014-05-17 09:51:15 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com> |
| 3 | |
| 4 | from __future__ import print_function |
| 5 | import sys |
| 6 | _python3 = sys.version_info.major == 3 |
| 7 | |
| 8 | |
| 9 | def to_hex(s): |
| 10 | if _python3: |
| 11 | return " ".join("0x{0:02x}".format(c) for c in s) # <-- Python 3 is OK |
| 12 | else: |
| 13 | return " ".join("0x{0:02x}".format(ord(c)) for c in s) |
| 14 | |
danghvu | 2412069 | 2014-09-27 17:59:42 -0500 | [diff] [blame^] | 15 | def to_hex2(s): |
| 16 | if _python3: |
| 17 | r = "".join("{0:02x}".format(c) for c in s) # <-- Python 3 is OK |
| 18 | else: |
| 19 | r = "".join("{0:02x}".format(ord(c)) for c in s) |
| 20 | while r[0] == '0': r = r[1:] |
| 21 | return r |
Nguyen Anh Quynh | 1098329 | 2014-05-17 09:51:15 +0800 | [diff] [blame] | 22 | |
| 23 | def to_x(s): |
| 24 | from struct import pack |
| 25 | if not s: return '0' |
| 26 | x = pack(">q", s) |
| 27 | while x[0] in ('\0', 0): x = x[1:] |
danghvu | 2412069 | 2014-09-27 17:59:42 -0500 | [diff] [blame^] | 28 | return to_hex2(x) |
Nguyen Anh Quynh | 1098329 | 2014-05-17 09:51:15 +0800 | [diff] [blame] | 29 | |
| 30 | def to_x_32(s): |
| 31 | from struct import pack |
| 32 | if not s: return '0' |
| 33 | x = pack(">i", s) |
| 34 | while x[0] in ('\0', 0): x = x[1:] |
danghvu | 2412069 | 2014-09-27 17:59:42 -0500 | [diff] [blame^] | 35 | return to_hex2(x) |