-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathByteUtil.py
106 lines (89 loc) · 3.07 KB
/
ByteUtil.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
__copyright__ = "Copyright (c) 2022-2025, Intelligent Imaging Innovations, Inc. All rights reserved. All rights reserved."
__license__ = "This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree."
import numpy as np
def uint16_to_bytes(inVal):
theArray = np.array([inVal],np.uint16)
theBytes = theArray.tobytes()
return theBytes
def int16_to_bytes(inVal):
theArray = np.array([inVal],np.int16)
theBytes = theArray.tobytes()
return theBytes
def uint32_to_bytes(inVal):
theArray = np.array([inVal],np.uint32)
theBytes = theArray.tobytes()
return theBytes
def int32_to_bytes(inVal):
theArray = np.array([inVal],np.int32)
theBytes = theArray.tobytes()
return theBytes
def uint64_to_bytes(inVal):
theArray = np.array([inVal],np.uint64)
theBytes = theArray.tobytes()
return theBytes
def int64_to_bytes(inVal):
theArray = np.array([inVal],np.int64)
theBytes = theArray.tobytes()
return theBytes
def float32_to_bytes(inVal):
theArray = np.array([inVal],np.float32)
theBytes = theArray.tobytes()
return theBytes
def float64_to_bytes(inVal):
theArray = np.array([inVal],np.float64)
theBytes = theArray.tobytes()
return theBytes
def string_to_bytes(inString):
theBytes = str.encode(inString)
return theBytes
def bytes_to_string(inBytes):
theString = inBytes.decode()
#print('inBytes ',inBytes, flush=True)
#print('theString',theString, flush=True)
return theString
def bytes_to_int32(inBytes):
theArr = np.frombuffer(inBytes,np.int32)
return theArr
def bytes_to_float32(inBytes):
theArr = np.frombuffer(inBytes,np.float32)
return theArr
def type_to_bytes(inVal,inType):
if(inType == 'u2'):
theBytes = uint16_to_bytes(inVal)
elif(inType == 'i2'):
theBytes = int16_to_bytes(inVal)
elif(inType == 'u4'):
theBytes = uint32_to_bytes(inVal)
elif(inType == 'i4'):
theBytes = int32_to_bytes(inVal)
elif(inType == 'u8'):
theBytes = uint64_to_bytes(inVal)
elif(inType == 'i8'):
theBytes = int64_to_bytes(inVal)
elif(inType == 'f4'):
theBytes = float32_to_bytes(inVal)
elif(inType == 'f8'):
theBytes = float64_to_bytes(inVal)
elif(inType == 's'):
theBytes = string_to_bytes(inVal)
return theBytes
def bytes_to_type(inBytes,inType):
if(inType == 'u2'):
theArr = np.frombuffer(inBytes,np.uint16)
elif(inType == 'i2'):
theArr = np.frombuffer(inBytes,np.int16)
elif(inType == 'u4'):
theArr = np.frombuffer(inBytes,np.uint32)
elif(inType == 'i4'):
theArr = np.frombuffer(inBytes,np.int32)
elif(inType == 'u8'):
theArr = np.frombuffer(inBytes,np.uint64)
elif(inType == 'i8'):
theArr = np.frombuffer(inBytes,np.int64)
elif(inType == 'f4'):
theArr = np.frombuffer(inBytes,np.float32)
elif(inType == 'f8'):
theArr = np.frombuffer(inBytes,np.float64)
elif(inType == 's'):
theArr = bytes_to_string(inBytes)
return theArr