note 42617 deleted from function.intval by cmb
| From: | cmb@php.net | Date: | Sun, 16 Feb 2020 13:51:18 +0000 |
| Subject: | note 42617 deleted from function.intval by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214150@lists.php.net to get a copy of this message | ||
Note Submitter:
----
When trying to read 32bit values (32 bit limitation depends on the word size of your machine) from
a string that is represented in hex with the high bit set,
eg. F9833234
intval returns -1. The reason is the sign bit being set. This number is larger than can be saved in
a signed int.
The way around this is to read the value in using two calls to intval.
eg.
$val = intval(substr($str,0,4), 16); // read high 16 bit word
$val <<= 16; // shift hi word correct position
$val |= intval(substr($str, 4, 4), 16); // read low 16 bit word