note 97172 added to function.hexdec
| From: | flurinj at gmx dot net | Date: | Tue, 06 Apr 2010 14:13:50 +0000 |
| Subject: | note 97172 added to function.hexdec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167726@lists.php.net to get a copy of this message | ||
Here My version of converting a hex string to a signed decimal value:
<?php
function hexdecs($hex)
{
// ignore non hex characters
$hex = preg_replace('/[^0-9A-Fa-f]/', '', $hex);
// converted decimal value:
$dec = hexdec($hex);
// maximum decimal value based on length of hex + 1:
// number of bits in hex number is 8 bits for each 2 hex -> max = 2^n
// use 'pow(2,n)' since '1 << n' is only for integers and therefore
limited to integer size.
$max = pow(2, 4 * (strlen($hex) + (strlen($hex) % 2)));
// complement = maximum - converted hex:
$_dec = $max - $dec;
// if dec value is larger than its complement we have a negative value (first bit is set)
return $dec > $_dec ? -$_dec : $dec;
}
?>
----
Server IP: 194.153.94.10
Probable Submitter: 77.59.251.170
----
Manual Page -- http://www.php.net/manual/en/function.hexdec.php
Edit -- https://master.php.net/note/edit/97172
Del: integrated -- https://master.php.net/note/delete/97172/integrated
Del: useless -- https://master.php.net/note/delete/97172/useless
Del: bad code -- https://master.php.net/note/delete/97172/bad+code
Del: spam -- https://master.php.net/note/delete/97172/spam
Del: non-english -- https://master.php.net/note/delete/97172/non-english
Del: in docs -- https://master.php.net/note/delete/97172/in+docs
Del: other reasons-- https://master.php.net/note/delete/97172
Reject -- https://master.php.net/note/reject/97172
Search -- https://master.php.net/manage/user-notes.php