commit 911a3082c22dac3e8bc6670a200683e4b9d9b21d
parent 60777d73db071448801a083901ec352513fd9dad
Author: Paco Esteban <paco@e1e0.net>
Date: Thu, 20 Feb 2020 22:45:00 +0100
new base conversion utility
Diffstat:
4 files changed, 164 insertions(+), 0 deletions(-)
diff --git a/baseConversion/.gitignore b/baseConversion/.gitignore
@@ -0,0 +1,14 @@
+b2d
+b2h
+b2o
+baseConv
+baseConv.o
+d2b
+d2h
+d2o
+h2b
+h2d
+h2o
+o2b
+o2d
+o2h
diff --git a/baseConversion/Makefile b/baseConversion/Makefile
@@ -0,0 +1,25 @@
+BIN = baseConv
+ALIASES = d2b d2h d2o h2d h2b h2o o2d o2h o2b b2d b2h b2o
+PREFIX = /usr/local
+
+.PHONY: clean install
+
+${BIN}: ${BIN}.o ${ALIASES}
+ ${CC} -o ${BIN} ${BIN}.o
+
+${ALIASES}:
+ ln -f -s ${BIN} $@
+
+install:
+ install -d -m 0755 ${PREFIX}/bin
+ install -d -m 0755 ${PREFIX}/man/man1
+ install -m 0755 ${BIN} ${PREFIX}/bin
+ install -m 0644 ${BIN}.1 ${PREFIX}/man/man1
+.for i in ${ALIASES}
+ ln -f -s ${PREFIX}/bin/${BIN} ${PREFIX}/bin/${i}
+.endfor
+
+clean:
+ rm -f ${BIN}
+ rm -f *.o
+ rm -f ${ALIASES}
diff --git a/baseConversion/baseConv.1 b/baseConversion/baseConv.1
@@ -0,0 +1,64 @@
+.Dd February 20, 2020
+.Dt BASECONV 1
+.Os
+.Sh NAME
+.Nm d2h ,
+.Nm d2o ,
+.Nm d2b ,
+.Nm h2d ,
+.Nm h2o ,
+.Nm h2b ,
+.Nm o2d ,
+.Nm o2h ,
+.Nm o2b ,
+.Nm b2d ,
+.Nm b2h ,
+.Nm b2o
+.Nd convert an integer between bases
+.Sh SYNOPSIS
+.Nm d2h
+.Ar base10_integer
+.Nm d2o
+.Ar base10_integer
+.Nm d2b
+.Ar base10_integer
+.Nm h2d
+.Ar base16_integer
+.Nm h2o
+.Ar base16_integer
+.Nm h2b
+.Ar base16_integer
+.Nm o2d
+.Ar base8_integer
+.Nm o2h
+.Ar base8_integer
+.Nm o2b
+.Ar base8_integer
+.Nm b2d
+.Ar base2_integer
+.Nm b2h
+.Ar base2_integer
+.Nm b2o
+.Ar base2_integer
+.Sh DESCRIPTION
+.Nm
+(and it's other aliases) convert an integer between bases.
+The behaviour is different depending on which name it is invoked.
+The binary names are ment to be nemonics, where the first letter is the base
+from (one of decimal, hexadecimal, octal or binary) and the third character is
+the base to.
+.Pp
+The options are as follows:
+.Bl -tag -width Ds
+.It Ar baseX_integer
+The integer you want to convert from.
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh AUTHORS
+.An Paco Esteban
+.Mt paco@e1e0.net
+.Sh BUGS
+Probably many.
+If you find one and want to send a patch, please to so to:
+.Mt patches@e1e0.net
diff --git a/baseConversion/baseConv.c b/baseConversion/baseConv.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <err.h>
+#include <limits.h>
+
+extern char* __progname;
+
+int
+main(int argc, char *argv[])
+{
+ char format[12] = "%ld\n";
+ uint8_t base = 10;
+ long converted;
+ char *endptr;
+
+ if (argc < 2 || strlen(__progname) != 3) {
+ fprintf(stderr, "usage: d2h | d2o | d2b <base10_int>\n");
+ fprintf(stderr, " h2d | h2o | h2b <base16_int>\n");
+ fprintf(stderr, " o2h | o2d | o2b <base8_int>\n");
+ fprintf(stderr, " b2h | b2o | b2d <base2_int>\n");
+ exit(1);
+ }
+
+ if (__progname[0] == 'h') base = 16;
+ if (__progname[0] == 'o') base = 8;
+ if (__progname[0] == 'b') base = 2;
+ if (__progname[2] == 'h')
+ (void)strcpy(format, "0x%02lx\n");
+ if (__progname[2] == 'o')
+ (void)strcpy(format, "0%02lo\n");
+
+ converted = strtol(argv[1], &endptr, base);
+
+ if (*endptr != '\0')
+ err(1, "invalid number");
+ if (errno == ERANGE && (converted == LONG_MAX || converted == LONG_MIN))
+ err(1, "out of range");
+
+ if (__progname[2] == 'b') {
+ long c, k;
+
+ for (c = (sizeof(converted) * 8) - 1; c >= 0; c--) {
+ k = converted >> c;
+
+ if (k & 1)
+ printf("1");
+ else
+ printf("0");
+
+ if (c % 4 == 0)
+ printf(" ");
+ }
+ printf("\n");
+ } else {
+ printf(format, converted);
+ }
+ return 0;
+}
+