-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathendian.py
52 lines (39 loc) · 1.26 KB
/
endian.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
#!/usr/bin/env python3
import struct
from typing import Union
def GETU32BE(data: Union[bytes, bytearray], offset: int) -> int:
"""
Get a 32-bit integer value from the given bytes / bytearray, starting at the given offset
:param data:
:param offset:
:return: value
"""
return struct.unpack('>L', data[offset:])
def GETU64BE(data: Union[bytes, bytearray], offset: int) -> int:
"""
Get a 64-bit integer value from the given bytes / bytearray, starting at the given offset
:param data:
:param offset:
:return: value
"""
return struct.unpack('>Q', data[offset:])
def SETU32BE(data: Union[bytes, bytearray], offset: int, value: int) -> None:
"""
:param data:
:param offset:
:param value:
:return:
"""
# value_bytes = struct.pack('>L', (value)) # Alternatively
value_bytes = value.to_bytes(32, 'big', signed=False)
data[offset:len(value_bytes)] = value_bytes
def SETU64BE(data: Union[bytes, bytearray], offset: int, value: int) -> None:
"""
:param data:
:param offset:
:param value:
:return:
"""
# value_bytes = struct.pack('>Q', (value)) # Alternatively
value_bytes = value.to_bytes(64, 'big', signed=False)
data[offset:len(value_bytes)] = value_bytes