note 108739 added to function.dechex
| From: | huangwen77 at gmail dot com | Date: | Tue, 22 May 2012 02:24:39 +0000 |
| Subject: | note 108739 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188125@lists.php.net to get a copy of this message | ||
For those who are looking for a function to calculate the hex of ANY length integer, here is my
snippet:
// The function to convert any length integers to hex
// Input: Decimal string
// Output: Hex string
function dec2hex($str)
{
$hex =
array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
// Result value
$hexval = '';
// The quotient of each division operation
$quotient = $str;
$divisor = $str;
while($quotient > 15)
{
$len = strlen($divisor);
$pos = 1;
$quotient = 0;
// Take the first two digits as temp divisor and advance by 1 each iteration
$div = substr($divisor, 0, 2);
$remainder = $div[0];
while($pos < $len)
{
$div = intval($remainder.$divisor[$pos]);
$remainder = $div % 16;
$quotient = intval($quotient.floor($div/16));
$pos++;
}
// Recast the divisor as string to make the $divisor[$pos] work
$divisor = "$quotient";
$hexval = $hex[$remainder].$hexval;
}
$hexval = $hex[$quotient].$hexval;
return $hexval;
}
----
Server IP: 69.147.83.197
Probable Submitter: 61.93.225.26
----
Manual Page -- http://www.php.net/manual/en/function.dechex.php
Edit -- https://master.php.net/note/edit/108739
Del: integrated -- https://master.php.net/note/delete/108739/integrated
Del: useless -- https://master.php.net/note/delete/108739/useless
Del: bad code -- https://master.php.net/note/delete/108739/bad+code
Del: spam -- https://master.php.net/note/delete/108739/spam
Del: non-english -- https://master.php.net/note/delete/108739/non-english
Del: in docs -- https://master.php.net/note/delete/108739/in+docs
Del: other reasons-- https://master.php.net/note/delete/108739
Reject -- https://master.php.net/note/reject/108739
Search -- https://master.php.net/manage/user-notes.php