|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-10-03 05:33 UTC] andy at vegebyte dot co dot uk
hexdec('012345') should return int(74565), but instead it returns float(74565), contrary to the documentation. This happens for all numbers in strings I've tested that begin with 0, which does not include non-hexadecimal characters which are taken to be 0, eg, hexdec('q12345') returns int.
(Fixing the problem by casting the result to an (int) works fine. It originally catched me out when I used array_flip on an array of results, which demands integer or string keys.)
I'm not sure what the configure line is, but it's just a regular install from the downloaded executable binary, with no extra modules loaded. Since maths functions are in the PHP core, it hopefully won't matter.
Thanks
Andrew Alderwick, United Kingdom
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 05:00:01 2025 UTC |
Interesting catch. The relevant code looks as following: for (i = Z_STRLEN_P(arg); i > 0; i--) { c = *s++; digit = (c >= '0' && c <= '9') ? c - '0' : (c >= 'A' && c <= 'Z') ? c - 'A' + 10 : (c >= 'a' && c <= 'z') ? c - 'a' + 10 : base; if (digit >= base) continue; switch (mode) { case 0: /* Integer */ onum = num; num = num * base + digit; fprintf(stderr, "num = %d, onum = %d\n", num, onum); if (num > onum) break; /* No overflow, continue */ fnum = onum; mode = 1; /* fall-through */ case 1: /* Float */ fnum = fnum * base + digit; } } from ext/standard/math.c ( _php_math_basetozval ). This code assumed that an overflow also occured when the newer 'num' == 'onum'. "A" fix would be to change the comparison to be ">=" instead of ">" . But I don't know the code well enough, someone else needs to check this toroughly.