note 108045 added to function.hexdec

From: Date: Sat, 24 Mar 2012 02:51:02 +0000
Subject: note 108045 added to function.hexdec
Groups: php.notes 
Request: Send a blank email to php-notes+get-186802@lists.php.net to get a copy of this message
If you want to create or parse signed Hex strings: <?php // $d should be an int function sdechex($d) { return ($d<0) ? ('-' . dechex(-$d)) : dechex($d); } // $h should be a string function shexdec($h) { return ($h[0] === '-') ? -('0x' . substr($h,1) + 0) : ('0x' . $h + 0); } // test $h = sdechex(-123); // string(3) "-7b" $d = shexdec($h); // int(-123) var_dump($h, $d); ?> Also note that ('0x' . $hexstr + 0) is faster than hexdec() (Tested on PHP v5.2.17) ---- Server IP: 69.147.83.197 Probable Submitter: 99.243.44.37 ---- Manual Page -- http://www.php.net/manual/en/function.hexdec.php Edit -- https://master.php.net/note/edit/108045 Del: integrated -- https://master.php.net/note/delete/108045/integrated Del: useless -- https://master.php.net/note/delete/108045/useless Del: bad code -- https://master.php.net/note/delete/108045/bad+code Del: spam -- https://master.php.net/note/delete/108045/spam Del: non-english -- https://master.php.net/note/delete/108045/non-english Del: in docs -- https://master.php.net/note/delete/108045/in+docs Del: other reasons-- https://master.php.net/note/delete/108045 Reject -- https://master.php.net/note/reject/108045 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186802) next »