-
Notifications
You must be signed in to change notification settings - Fork 11
/
integerencoder.py
63 lines (43 loc) · 1.46 KB
/
integerencoder.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
61
62
63
#!/usr/bin/env python
# Author Dario Clavijo 2018
import struct
def uint32tobytes(value, big_endian=True):
return struct.pack(">I", value) if big_endian else struct.pack("<I", value)
def bytestouint32(value, big_endian=True):
if big_endian:
return struct.unpack(">I", value)[0]
else:
return struct.unpack("<I", value)[0]
def int32tobytes(value, big_endian=True):
return struct.pack(">i", value) if big_endian else struct.pack("<i", value)
def bytestoint32(value, big_endian=True):
if big_endian:
return struct.unpack(">i", value)[0]
else:
return struct.unpack("<i", value)[0]
def test1():
print((uint32tobytes(0).encode("hex")))
print((uint32tobytes(1).encode("hex")))
print((uint32tobytes(0, False).encode("hex")))
print((uint32tobytes(1, False).encode("hex")))
print((int32tobytes(0).encode("hex")))
print((int32tobytes(-1).encode("hex")))
print((int32tobytes(0, False).encode("hex")))
print((int32tobytes(-1, False).encode("hex")))
def test2():
print((bytestoint32(int32tobytes(c[0] + c[1]))))
# test1()
# test2()
regs = [0 * 31]
regs[0] = int32tobytes(-1)
print(regs)
a = (bytestouint32(regs[0]) >> 1) & 0xFFFFFFFF
b = (bytestouint32(regs[0]) << 1) & 0xFFFFFFFF
# print len(a),len(b)
print(a, b)
print(hex(a), hex(b))
print(uint32tobytes(a).encode("hex"))
print(int32tobytes(a).encode("hex"))
print(uint32tobytes(b).encode("hex"))
print(int32tobytes(b).encode("hex"))
# print(a << 1)