Note that using base=0
, strtoul() will recognize decimal, octal, and hex formats,
but not binary.
To convert from binary format use base=2
and skip the "0b" part,
e.g. char *e; unsigned long int xl = strtoul("101",&e,2);
will set xl
to 5 and *e
will be 0.
Assume that unsigned int uses 32 bits.
To check for strtoul() failure, #include <errno.h>
and before calling strtoul()
set errno=0;
then after calling strtoul(), if errno
is not zero
call perror("strtoul");
.
For other bad arguments print an error message to standard error, i.e.
fprintf( stderr, "format...", ...);
Test with some good arguments: 12 012 0x12 0b101100111
and bad arguments: 12efg 0129 0xefg 0b123 0xfffffffff 0xffffffffffffffffffffffffffff
Sample run:
$ ./bits 0x1234 123abcefg 0b101100111000 0x1234 = 4660 = 0x00001234 = 0b00000000000000000001001000110100 bitrev = 742916096 = 0x2c480000 = 0b00101100010010000000000000000000 convert: bad argument: 123abcefg 123abcefg = 123 = 0x0000007b = 0b00000000000000000000000001111011 bitrev = 3724541952 = 0xde000000 = 0b11011110000000000000000000000000 0b101100111000 = 2872 = 0x00000b38 = 0b00000000000000000000101100111000 bitrev = 483393536 = 0x1cd00000 = 0b00011100110100000000000000000000 $References: args, bitops, strtoul(), perror()
Programs must compile with no warnings or errors using: gcc -Wall
Each source file must start with a comment containing your name and a description.