note 83122 added to function.dechex

From: Date: Sun, 11 May 2008 16:55:55 +0000
Subject: note 83122 added to function.dechex
Groups: php.notes 
Request: Send a blank email to php-notes+get-139283@lists.php.net to get a copy of this message
I was challenged by a problem with large number calculations and conversion to hex within php. The calculation exceeded unsigned integer and even float range. You can easily change it for your needs but it is, thanks to bcmath, capable of handling big numbers via string. This function will convert them to hex. In this specific example though, since I use it for game internals that can only handle 32 bit numbers, it will truncate calculations at 8 digits. If the input is 1 for example it will be filled up with zeros. Output 00000001h. Of course I don't claim it to be a good one, but it works for me and my purpose. Suggestions on faster code welcome! // Turns numbers into 32-bit hex string; Fills up zeros function lrgDec2Hex($number) { $i = 0; $hex = array(); while($i < 8) { if($number == 0) { array_push($hex, '0'); } else { array_push($hex, strtoupper(dechex(bcmod($number, '16')))); $number = bcdiv($number, '16', 0); } $i++; } krsort($hex); return implode($hex); } ---- Server IP: 69.147.83.197 Probable Submitter: 87.161.93.13 ---- Manual Page -- http://www.php.net/manual/en/function.dechex.php Edit -- https://master.php.net/note/edit/83122 Del: integrated -- https://master.php.net/note/delete/83122/integrated Del: useless -- https://master.php.net/note/delete/83122/useless Del: bad code -- https://master.php.net/note/delete/83122/bad+code Del: spam -- https://master.php.net/note/delete/83122/spam Del: non-english -- https://master.php.net/note/delete/83122/non-english Del: in docs -- https://master.php.net/note/delete/83122/in+docs Del: other reasons-- https://master.php.net/note/delete/83122 Reject -- https://master.php.net/note/reject/83122 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139283) next »