Skip to content

Commit cd7c271

Browse files
committed
Use int.from_bytes rather than doing bitwise operations to parse ints. This might be marginally slower, but it makes the code easier to read
1 parent 0878e8c commit cd7c271

1 file changed

Lines changed: 3 additions & 12 deletions

File tree

  • software/firmware/experimental

software/firmware/experimental/osc.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __init__(self, data: bytes):
9292
t = chr(data[i])
9393
if t == "i":
9494
types.append(int)
95-
n = (data[d] << 24) | (data[d + 1] << 16) | (data[d + 2] << 8) | data[d + 3]
95+
n = int.from_bytes(data[d:d + 4], "big")
9696
self.values.append(n)
9797
d += 4
9898
elif t == "f":
@@ -112,7 +112,7 @@ def __init__(self, data: bytes):
112112
elif t == "b":
113113
# blob; int32 -> n, followed by n bytes
114114
types.append(bytearray)
115-
n = (data[d] << 24) | (data[d + 1] << 16) | (data[d + 2] << 8) | data[d + 3]
115+
n = int.from_bytes(data[d:d + 4], "big")
116116
d += 4
117117
b = []
118118
for i in range(n):
@@ -130,16 +130,7 @@ def __init__(self, data: bytes):
130130
# 64-bit signed integer
131131
# treat as a normal int
132132
types.append(int)
133-
n = (
134-
(data[d] << 56)
135-
| (data[d + 1] << 48)
136-
| (data[d + 2] << 40)
137-
| (data[d + 3] << 32)
138-
| (data[d + 4] << 24)
139-
| (data[d + 5] << 16)
140-
| (data[d + 6] << 8)
141-
| data[d + 7]
142-
)
133+
n = int.from_bytes(data[d:d + 8], "big")
143134
self.values.append(n)
144135
d += 8
145136
elif t == "c":

0 commit comments

Comments
 (0)