summaryrefslogtreecommitdiffstatshomepage
path: root/py/mpz.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/mpz.c')
-rw-r--r--py/mpz.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/py/mpz.c b/py/mpz.c
index b3d8806792..7515930538 100644
--- a/py/mpz.c
+++ b/py/mpz.c
@@ -1609,7 +1609,6 @@ bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) {
return true;
}
-// writes at most len bytes to buf (so buf should be zeroed before calling)
void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) {
byte *b = buf;
if (big_endian) {
@@ -1641,6 +1640,15 @@ void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) {
}
}
}
+
+ // fill remainder of buf with zero/sign extension of the integer
+ if (big_endian) {
+ len = b - buf;
+ } else {
+ len = buf + len - b;
+ buf = b;
+ }
+ memset(buf, z->neg ? 0xff : 0x00, len);
}
#if MICROPY_PY_BUILTINS_FLOAT