hello
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
char *str = "ffffffff";
int c;
c = strtol(str, 0, 16);
if (errno != 0)
printf("Strtol overflowed!!!\n");
printf("%d\n", c);
return 0;
}
How do I make strtol() read ffffffff as -1 and not UNSIGNED_LONGMAX??
I'm basically making a conversion program that converts between
different bases, e.g decimal to hex, hex to decimal, decimal to binary, etc.
The user can enter ---> cvt -h ffffffff -d and it prints -1
If the user enters --> cvt -uh ffffffff -d (u stands for unsigned) it prints whatever
UNSIGNED LONG MAX is.
The above overflows because it's reading ffffffff as UNSIGNED_LONGMAX.
Similarly with:
Code:
char *srt = "11111111";
c = strtol(str, 0, 2);
strtol reads 11111111 as 255. Is there a way to make it read 11111111 as -1?
I know there are various ways without using strtol. Im just wondering if strtol
has some built in mechanism for reading 11111111 or ffffffff as negative instead
of positive.
thanks