summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/py/binary.c b/py/binary.c
index a738dd62a9..71d715c3e1 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -11,6 +11,36 @@
// Helpers to work with binary-encoded data
+int mp_binary_get_size(char typecode) {
+ // This assumes that unsigned and signed types are of the same type,
+ // which is invariant for [u]intN_t.
+ switch (typecode) {
+ case BYTEARRAY_TYPECODE:
+ case 'b':
+ case 'B':
+ return sizeof(int8_t);
+ case 'h':
+ case 'H':
+ return sizeof(int16_t);
+ case 'i':
+ case 'I':
+ return sizeof(int32_t);
+ case 'l':
+ case 'L':
+ return sizeof(int32_t);
+ case 'q':
+ case 'Q':
+ return sizeof(long long);
+#if MICROPY_ENABLE_FLOAT
+ case 'f':
+ return sizeof(float);
+ case 'd':
+ return sizeof(double);
+#endif
+ }
+ return -1;
+}
+
mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
int val = 0;
switch (typecode) {