From: Huck Boles Date: Wed, 21 Dec 2022 06:00:54 +0000 (-0600) Subject: Wed Dec 21 12:00:53 AM CST 2022 automatic backup X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=c9131747fa67b44d2ad288fdb9f813f3c9ef58a2;p=base.git Wed Dec 21 12:00:53 AM CST 2022 automatic backup --- diff --git a/README.md b/README.md index dc1beff..7f6d950 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,33 @@ # *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 : @@ -32,12 +35,14 @@ If not specified, base will print binary numbers as unsigned longs with 64 bits, $ unzip base-{branch}.zip - ***OR*** + ***OR*** $ tar -I gzip -cvf base-{branch}.tar.gz - Build base from source $ cd base-{branch} + + $ sudo make install diff --git a/base.h b/base.h index 5aadfc0..7fff267 100644 --- a/base.h +++ b/base.h @@ -4,8 +4,8 @@ #include #include -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); diff --git a/function.c b/function.c index 8010959..f02ba05 100644 --- a/function.c +++ b/function.c @@ -3,7 +3,7 @@ /* 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); @@ -18,38 +18,54 @@ void printnum(char *bin, int ibase, int obase, int len){ 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){ diff --git a/main.c b/main.c index 1309614..70ee21e 100644 --- a/main.c +++ b/main.c @@ -4,23 +4,24 @@ void help(void); 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(); @@ -28,6 +29,9 @@ int main(int argc, char *argv[]){ case 'l': strcpy(l,optarg); break; + case 'L': + strcpy(L,optarg); + break; case 'i': strcpy(i,optarg); break; @@ -58,12 +62,18 @@ int main(int argc, char *argv[]){ } 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; }