# *base* - CLI utility to convert numbers between arbitrary bases.
*base* converts from base 10 to binary if called with no options. Input and output base can be specified with -i and -o respectively, ranging from 2-62. If no number is given, base will prompt for a number, or convert stdin if given.
-If not specified, base will print binary numbers as unsigned longs with 64 bits, hexadecimal numbers with a leading 0x, octal and any other number with no leading zeroes. Truncation/leading zeroes can be specified with -l and -L, giving the number of digits from the lowest or highest number, respectively.
+If not specified, base will print binary numbers as unsigned longs with 64 bits, hexadecimal numbers with a leading 0x, octal and any other number with no leading zeroes. Truncation/leading zeroes can be specified with -l and -L, giving the starting position from lowest or highest digit respectively.
## Usage:
- Print binary representation:
$ base 555
- 00000000 00000000 00000000 00000000 00000000 00000000 00000010 00101011
+
+ 00000000 00000000 00000000 00000000 00000000 00000000 00000010 00101011
- Print a number in hexadecimal:
$ base -o 16 1234
- 0x4d2
+
+ 0x4d2
- Print an octal representation of a base 12 number
$ base -o 8 -i 12 5a
- 106
+
+ 106
- Convert stdin to base 8
$ echo "123456" | base -o 8
- 361100
+ 361100
## Installation :
$ unzip base-{branch}.zip
- ***OR***
+ ***OR***
$ tar -I gzip -cvf base-{branch}.tar.gz
- Build base from source
$ cd base-{branch}
+
+
$ sudo make install
#include <string.h>
#include <unistd.h>
-void printbin(int, int);
-void printnum(char *, int, int, int);
-void printbase(int, int);
+void printbin(int, int, int);
+void printnum(char *, int, int, int, int);
+void printbase(int, int, int, int);
int getnum(char *, int);
/* enumerate up to base 64 */
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
-void printnum(char *bin, int ibase, int obase, int len){
+void printnum(char *bin, int ibase, int obase, int start, int end){
int out = getnum(bin,ibase);
printf("%o\n", out);
break;
case (2):
- printbin(out,len);
+ printbin(out,start,end);
break;
default:
- printbase(out,obase);
+ printbase(out,obase,start,end);
break;
}
}
-void printbase(int num, int base){
+void printbase(int num, int base, int start, int end){
int n = 0;
char *s = malloc(100*sizeof(char));
while (num > 0){
s[n++] = digits[num%base];
num /= base;
}
- for (int i = n - 1; i >= 0; i--){
- printf("%c",s[i]);
- printf("%s",(i%3==0) ? " " : "");
+
+ if (end != 0) {
+ /* print leading zeroes if needed */
+ if (end >= n) {
+ while (end-- >= n) printf("0");
+ /* truncate n if there are too many digits */
+ } else {
+ n = end;
+ }
}
+
+ /* print in reverse order of magnitude */
+ for (int i = n - 1; i >= start; printf("%c",s[i--]))
+
+ while (start-- > 0) printf("0");
+
printf("\n");
+ return;
}
-void printbin(int i, int n){
- unsigned long mask = pow(2,n-1);
+void printbin(int i, int start, int end){
+ unsigned long mask = pow(2,(end != 0) ? end-1 : 63);
- for (int t = n-1; t >= 0; t-- ){
+ for (int t = (end !=0) ? end : 63; t >= start; t-- ){
printf("%i",((i & mask) == 0) ? 0 : 1);
printf("%s",(t % 8 == 0) ? " " : "");
mask = mask >> 1;
}
- printf("\n");
+ while (start-- > 0) printf("0");
+
+ printf("\n");
+ return;
}
int getnum(char *num, int base){
int main(int argc, char *argv[]){
char *l = malloc(20*sizeof(char));
+ char *L = malloc(20*sizeof(char));
char *i = malloc(20*sizeof(char));
char *o = malloc(20*sizeof(char));
char *n = malloc(60*sizeof(char));
char c;
- int len,ibase,obase;
+ int ibase,obase,start,end;
/* print stdin in binary if called alone */
if (argc == 1) {
scanf("%s",n);
- printbin(strtol(n, NULL, 10),64);
+ printbin(strtol(n, NULL, 10),0,0);
return 0;
/* print base 10 number in binary if given with no arguments */
} else if (argc == 2) {
- printbin(strtol(argv[1],NULL,10),64);
+ printbin(strtol(argv[1],NULL,10),0,0);
return 0;
} else {
- while ((c = getopt (argc, argv, "ho:i:l:n:")) != -1){
+ while ((c = getopt (argc, argv, "ho:i:l:L:n:")) != -1){
switch (c) {
case 'h':
help();
case 'l':
strcpy(l,optarg);
break;
+ case 'L':
+ strcpy(L,optarg);
+ break;
case 'i':
strcpy(i,optarg);
break;
}
if (strcmp(l,"") != 0) {
- len = strtol(l,NULL,10);
+ start = strtol(l,NULL,10);
+ } else {
+ start = 0;
+ }
+
+ if (strcmp(L,"") != 0) {
+ end = strtol(l,NULL,10);
} else {
- len = 64;
+ end = 0;
}
- printnum(n,ibase,obase,len);
+ printnum(n,ibase,obase,start,end);
return 0;
}