/* * print numbers using any base * Input number can be at any format (see strtol(3C)) * :set tabstop=4 */ #include #include #include #include #define PATH_SEP '/' static char __ident[] = "@(#)(c)SPDsoft, Wed Jan 24, 1996"; #define VERS_STR ((char*)&__ident[4]) char *out_c(unsigned char c); static char s[9]; void usage(void); extern int errno; char *app; void usage(void) { fprintf(stderr,"Use: %s [-hXxbodc] [-B base] number [...]\n", app); fprintf(stderr,"%s: -x : hex\n", app); fprintf(stderr,"%s: -b : bin\n", app); fprintf(stderr,"%s: -o : octal\n", app); fprintf(stderr,"%s: -d : dec\n", app); fprintf(stderr,"%s: -c : char\n", app); exit(0); } main( int argc, char *argv[]) { int opt; char *fmt="0x%0x\n\0x00 "; long num; int base=0; extern char *optarg; extern int optind,opterr; if ((app = strrchr(argv[0], PATH_SEP)) != NULL) app = app+1; else app = argv[0]; while ( (opt=getopt(argc,argv,"hB:Xxodcb")) != EOF ) { switch(opt) { case 'B': base=atoi(optarg); break; case 'x': break; case 'X': strcpy(fmt,"0x%X\n"); break; case 'o': strcpy(fmt,"0%o\n"); break; case 'd': strcpy(fmt,"%d\n"); break; case 'c': strcpy(fmt,"%c\n"); break; case 'b': fmt=NULL; break; case 'h': default: usage(); break; } } while (argv[optind]) { num = strtol( argv[optind], (char **)NULL, base ); if ( fmt != NULL ) { printf(fmt, num); } else { unsigned char *c; for ( c = (char *)# c < (char *)&num + sizeof(num); c++ ) printf( "%s%c", out_c(*c), c == (char *)&num + sizeof(num) - 1 ? '\n' : ' ' ); } optind++; } } char *out_c(unsigned char c) { char *p=s+7; do { *p--="01"[c&0x01]; c>>=1; } while ( p>=s ); return(s); }