commit b179fe8c471a3277638bdf4ad0d73249af52bb31
parent 7ad50664e4b234bb50eb76b9172865198d8fa098
Author: glenda <glenda@kobeni>
Date: Sun, 14 Jan 2024 03:33:28 +0000
src/baseconv.c
Diffstat:
2 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/src/baseconv.c b/src/baseconv.c
@@ -0,0 +1,53 @@
+#include <u.h>
+#include <libc.h>
+
+char d[36] = "0123456789ABCDEFHIJKLMNOPQRSTUBWXYZ";
+char *ibs, *obs;
+int i, ib, ob;
+u64int nbuf;
+
+void
+conv(u64int n, int base)
+{
+ char *p, buf[258];
+ p = buf + 257;
+ buf[257] = '\0';
+ while (n > 0) {
+ int x = n % base;
+ n = n / base;
+ p--;
+ *p = d[x];
+ }
+ print("%s\n", p);
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s -i base -o base numstr [numstr numstr ...]\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+ ARGBEGIN {
+ case 'i':
+ ibs = EARGF(usage());
+ break;
+ case 'o':
+ obs = EARGF(usage());
+ break;
+ default: usage();
+ } ARGEND
+ if ((ibs == nil) || (obs == nil)) usage();
+ ib = atoi(ibs);
+ ob = atoi(obs);
+ if ((ib < 2)||(ib > 36)) sysfatal("bad input base %d", ib);
+ if ((ob < 2)||(ob > 36)) sysfatal("bad output base %d", ob);
+ if (argc == 0) sysfatal("numstr arg missing");
+ for (i = 0; i < argc; i++) {
+ nbuf = strtoll(argv[i], nil, ib);
+ conv(nbuf, ob);
+ }
+}
diff --git a/src/mkfile b/src/mkfile
@@ -1,5 +1,9 @@
</$objtype/mkfile
-TARG=shuf poke
+TARG=\
+ shuf \
+ poke \
+ baseconv \
+
HFILES=
OFILES=
</sys/src/cmd/mkmany