diff options
author | aiju <devnull@localhost> | 2015-12-20 13:45:28 +0100 |
---|---|---|
committer | aiju <devnull@localhost> | 2015-12-20 13:45:28 +0100 |
commit | bdc2b7556896a069f71dcafb9d4ba4e94872bcfe (patch) | |
tree | 826c6b89aeba3e536667876fe6264110f915cd7f /sys/src/libmp | |
parent | a8f8de1cdeec7f9bee287c137b576e0249e8df82 (diff) |
mpfmt: handle base 2, 4
Diffstat (limited to 'sys/src/libmp')
-rw-r--r-- | sys/src/libmp/port/mpfmt.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/sys/src/libmp/port/mpfmt.c b/sys/src/libmp/port/mpfmt.c index 6c91e247d..3e18a8cf1 100644 --- a/sys/src/libmp/port/mpfmt.c +++ b/sys/src/libmp/port/mpfmt.c @@ -44,21 +44,22 @@ to32(mpint *b, char *buf, int len) static char set16[] = "0123456789ABCDEF"; static int -to16(mpint *b, char *buf, int len) +topow2(mpint *b, char *buf, int len, int s) { mpdigit *p, x; - int i, j; + int i, j, sn; char *out, *eout; if(len < 1) return -1; + sn = 1<<s; out = buf; eout = buf+len; for(p = &b->p[b->top-1]; p >= b->p; p--){ x = *p; - for(i = Dbits-4; i >= 0; i -= 4){ - j = 0xf & (x>>i); + for(i = Dbits-s; i >= 0; i -= s){ + j = x >> i & sn - 1; if(j != 0 || out != buf){ if(out >= eout) return -1; @@ -190,11 +191,17 @@ mptoa(mpint *b, int base, char *buf, int len) break; default: case 16: - rv = to16(b, out, len); + rv = topow2(b, out, len, 4); break; case 10: rv = to10(b, out, len); break; + case 4: + rv = topow2(b, out, len, 2); + break; + case 2: + rv = topow2(b, out, len, 1); + break; } if(rv < 0){ if(alloced) |