aboutsummaryrefslogtreecommitdiff
path: root/py/objint.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-06-15 13:56:21 +1000
committerDamien George <damien.p.george@gmail.com>2017-06-15 13:56:21 +1000
commit8c5632a869f10445dc5091c39ec73ca49cf83454 (patch)
tree932b9754b867c3173d899d53fca804a1bd03ffb6 /py/objint.c
parent2bf5a947b2b01f2be6c7b05707391dd09b240ad0 (diff)
py/objint: Support "big" byte-order in int.to_bytes().
Diffstat (limited to 'py/objint.c')
-rw-r--r--py/objint.c11
1 files changed, 4 insertions, 7 deletions
diff --git a/py/objint.c b/py/objint.c
index bda9c46cf..4801baa85 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -432,15 +432,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_fro
STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(int_from_bytes_obj, MP_ROM_PTR(&int_from_bytes_fun_obj));
STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
- // TODO: Support byteorder param
// TODO: Support signed param (assumes signed=False)
(void)n_args;
- if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) {
- mp_not_implemented("");
- }
-
mp_uint_t len = MP_OBJ_SMALL_INT_VALUE(args[1]);
+ bool big_endian = args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little);
vstr_t vstr;
vstr_init_len(&vstr, len);
@@ -449,12 +445,13 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *args) {
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
if (!MP_OBJ_IS_SMALL_INT(args[0])) {
- mp_obj_int_to_bytes_impl(args[0], false, len, data);
+ mp_obj_int_to_bytes_impl(args[0], big_endian, len, data);
} else
#endif
{
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(args[0]);
- mp_binary_set_int(MIN((size_t)len, sizeof(val)), false, data, val);
+ size_t l = MIN((size_t)len, sizeof(val));
+ mp_binary_set_int(l, big_endian, data + (big_endian ? (len - l) : 0), val);
}
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);