-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatatypes.py
More file actions
96 lines (79 loc) · 3.2 KB
/
datatypes.py
File metadata and controls
96 lines (79 loc) · 3.2 KB
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
import struct
import array
import ctypes
def bytes2int8(bytes):
''' Little endian parser which takes 1 unsigned integer and converts it to a single signed char. '''
a = array.array('B', bytes)
return struct.unpack('b', a)[0]
def bytes2uint8(bytes):
''' Little endian parser which takes 1 unsigned integer and converts it to a single unsigned char. '''
a = array.array('B', bytes)
return struct.unpack('B', a)[0]
def bytes2int16(bytes):
''' Little endian parser which takes a python list of 2 unsigned integers and converts them to a single signed short. '''
a = array.array('B', bytes)
return struct.unpack('h', a)[0]
def bytes2uint16(bytes):
''' Little endian parser which takes a python list of 2 unsigned integers and converts them to a single unsigned short. '''
a = array.array('B', bytes)
return struct.unpack('H', a)[0]
def bytes2int32(bytes):
''' Little endian parser which takes a python list of 4 unsigned integers and converts them to a single signed int. '''
a = array.array('B', bytes)
return struct.unpack('i', a)[0]
def bytes2uint32(bytes):
''' Little endian parser which takes a python list of 4 unsigned integers and converts them to a single unsigned int. '''
a = array.array('B', bytes)
return struct.unpack('I', a)[0]
def bytes2int64(bytes):
''' Little endian parser which takes a python list of 8 unsigned integers and converts them to a single long. '''
a = array.array('B', bytes)
return struct.unpack('l', a)[0]
def bytes2uint64(bytes):
''' Little endian parser which takes a python list of 8 unsigned integers and converts them to a single unsigned long. '''
a = array.array('B', bytes)
return struct.unpack('L', a)[0] #pandas: .astype(ctypes.c_ulong)
def bytes2float(bytes):
''' Little endian parser which takes a python list of 4 unsigned integers and converts them to a single float. '''
a = array.array('B', bytes)
return struct.unpack('f', a)[0]
def bytes2double(bytes):
''' Little endian parser which takes a python list of 8 unsigned integers and converts them to a single double. '''
a = array.array('B', bytes)
return struct.unpack('d', a)[0]
def bytes2bool(bytes):
''' Little endian parser which takes a python list of 1 unsigned integers and converts them to a single boolean. '''
a = array.array('B', bytes)
return struct.unpack('?', a)[0]
def bytes2hexstring(bytes):
"Return series of bytes as a hex string"
return ' '.join(map(lambda x: hex(x), bytes))
def bytes2type(type, bytes=None):
''' Takes a type string (see code below for options) and bytes (optional) and returns the bytes
converted to the given type. If bytes is not provided, the conversion function itself is returned
instead.'''
typeConversionFunctions = \
{ \
'UINT8': bytes2uint8, \
'INT8': bytes2int8, \
'UINT16': bytes2uint16, \
'INT16': bytes2int16, \
'UINT32': bytes2uint32, \
'INT32': bytes2int32, \
'UINT64': bytes2uint64, \
'INT64': bytes2int64, \
'FLOAT': bytes2float, \
'DOUBLE': bytes2double, \
'BOOL': bytes2bool, \
'HEXSTRING': bytes2hexstring \
}
type2Compare = type.strip().upper()
try:
func = typeConversionFunctions[type2Compare]
except:
print "No such type defined for: '" + str(type2Compare) + "'"
raise
if bytes:
return func(bytes)
else:
return func