-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.py
51 lines (40 loc) · 1.65 KB
/
converter.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
"""
PlzFixEP is an open-source helper to import data into Engine Prime
Copyright (C) 2020 Quentin PIERRE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import struct
import binascii
"""
A bunch of utils to convert binaries etc.
Makes my eyes bleed
"""
def hex_to_bin(hex_str):
return (int(hex_str, 16)).to_bytes(len(hex_str)//2, byteorder='big')
def bin_to_double_little_endian(bin_str):
return struct.unpack('<d', bin_str)[0]
def bin_to_double_big_endian(bin_str):
return struct.unpack('>d', bin_str)[0]
def bin_to_sint_little_endian(bin_str):
return struct.unpack('<i', bin_str)[0]
def bin_to_sint_big_endian(bin_str):
return struct.unpack('>i', bin_str)[0]
def float_to_double_little_endian(float_val):
return struct.pack('<d', float_val)
def float_to_double_big_endian(float_val):
return struct.pack('>d', float_val)
def sint_to_little_endian(sint_val):
return struct.pack('<i', sint_val)
def sint_to_big_endian(sint_val):
return struct.pack('>i', sint_val)
def read_bytes_from_hexstring(string, nb_bytes):
return hex_to_bin(string[:nb_bytes*2]), string[nb_bytes*2:]