diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 15:37:35 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 15:37:35 -0400 |
commit | 68e454c6b67bd26f11bfedc6a571f41bfa13d3d9 (patch) | |
tree | 12d267807c65b28208efacc7aa503f8d99a8d1cf | |
parent | 03adbf0757f7bd96576d8235c29f08e0e78d1003 (diff) |
modernize bigtostrx()
-rw-r--r-- | big.c | 34 |
1 files changed, 21 insertions, 13 deletions
@@ -959,31 +959,39 @@ word bigtostr(word x) } } -word bigtostrx(x) /* integer to hexadecimal string (as Miranda list) */ -word x; +/* integer to hexadecimal string (as Miranda list) */ +word bigtostrx(word x) { extern char *dicp; - word r = NIL, s = big_is_negative(x); + word r = NIL; + word s = big_is_negative(x); + while (x) { word count = 4; /* 60 bits => 20 octal digits => 4 bignum digits */ unsigned long long factor = 1; unsigned long long hold = 0; - while (count-- && x) /* calculate value of (upto) 4 bignum digits */ - hold = hold + factor * digit0(x), - /* printf("::%llx\n",hold), /* DEBUG */ - factor <<= 15, x = rest(x); + + /* calculate value of (upto) 4 bignum digits */ + while (count-- && x) { + hold = hold + factor * digit0(x); + factor <<= 15; + x = rest(x); + } + sprintf(dicp, "%.15llx", hold); /* 15 hex digits = 60 bits */ - /* printf(":::%s\n",dicp); /* DEBUG */ char *q = dicp + 15; - while (--q >= dicp) + while (--q >= dicp) { r = cons(*q, r); + } } - while (digit(r) == '0' && rest(r) != NIL) + + while (digit(r) == '0' && rest(r) != NIL) { r = rest(r); /* remove redundant leading 0's */ + } + r = cons('0', cons('x', r)); - if (s) - r = cons('-', r); - return (r); + + return s ? cons('-', r) : r; } word bigtostr8(x) /* integer to octal string (as Miranda list) */ |