On Tue, Apr 01, 2003 at 01:03:06PM -0500, Rodent of Unusual Size wrote:
> is this expected behaviour?

Yes

> perl -e 'use POSIX; printf "%08x\n", POSIX::strtol("0xdeadbeef");'
> 7fffffff
> 
> perl -e 'use POSIX; printf "%08x\n", POSIX::strtol("0x0eadbeef");'
> 0eadbeef

In C:

$ perl -we 'use Inline C => q(void test (char *src) {printf ("%s: %08x\n", src, strtol 
(src, NULL, 0));}); test $_ foreach @ARGV' 0xdeadbeef 0x0eadbeef
0xdeadbeef: 7fffffff
0x0eadbeef: 0eadbeef

> i haven't looked into it closely, but i suspect sign-bit bogosity.

RETURN VALUES
     The strtol() function returns the result of the conversion, unless the
     value would underflow or overflow.  If an underflow occurs, strtol()
     returns LONG_MIN.  If an overflow occurs, strtol() returns LONG_MAX.  The
     strtoll() function returns the result of the conversion, unless the value
     would underflow or overflow.  If an underflow occurs, strtoll() returns
     LLONG_MIN.  If an overflow occurs, strtoll() returns LLONG_MAX.  In all
     cases, errno is set to ERANGE.

strtoul probably does what you want. hex will even try to use doubles if
it runs out of space in an unsigned long.

Nicholas Clark

Reply via email to