// modified args.c #include #include // for strtoul() #include // void bprint( unsigned int x) { for( int i = 31; i >= 0; --i) printf( "%i", (x >> i) & 1); } void convert( char s[]) { printf( "%s",s); errno = 0; char *e; unsigned long il; // s+2 same as &s[2] if( s[0] == '0' && s[1] == 'b') il = strtoul( s+2, &e, 2); // base 2 else il = strtoul( s, &e, 0); // handles octal, decimal, and hex if( errno != 0) { perror("strtoul"); } if( *e != 0) { fprintf( stderr, "bad input\n"); } unsigned int i = il; // if i != il then error... printf( " = %u = 0x%x = ", i, i); // %u format for unsigned int bprint(i); printf("\n"); // reverse the bits in i and then print in decimal, hex, and binary } int main( int argc, char *argv[]) { for( int i = 1; i < argc; ++i) convert(argv[i]); return 0; } /* sample run: $ ./args 12 011 0xff 0b101 12 = 12 = 0xc = 00000000000000000000000000001100 011 = 9 = 0x9 = 00000000000000000000000000001001 0xff = 255 = 0xff = 00000000000000000000000011111111 0b101 = 5 = 0x5 = 00000000000000000000000000000101 $ */