M
Martin
Richard Bos said:Why do work which the library will do for you?
#include <limits.h>
#include <stdlib.h>
unsigned int htoi(char *txt)
{
unsigned long tmp=strtoul(txt, 0, 16);
if (tmp>UINT_MAX) return UINT_MAX;
return tmp;
}
Shorter, sweeter, doesn't waste value space on
negative numbers which you don't parse anyway,
doesn't invoke undefined behaviour on overflow,
and is portable no matter what your character set it.
Feel free.
Isn't there a possibility that UINT_MAX == ULONG_MAX?
Martin
http://martinobrien.co.uk/