note 108794 added to function.dechex
| From: | huangwen77 at gmail dot com | Date: | Fri, 25 May 2012 02:39:51 +0000 |
| Subject: | note 108794 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188215@lists.php.net to get a copy of this message | ||
A friend of mine pointed out that I made some mistakes on the post I made earlier so I have
re-written my php function for ANY length dec-to-hex conversion. The function take a string input
and return a hex which is represented by a string. Note that I assumed that the user's input is
a valid number thus I did NOT include any input verifying mechanism. So you have to do the verifying
part yourself.
The administrator of www.php.net please remove my post earlier, thanks.
<?php
// ==================== Support functions ====================
// The backoff function during contention
function backoff()
{
$miliSeconds = rand(0, 100);
//Sleep for 0 - 100 miliseconds, to avoid collision
usleep(round($miliSeconds*1000));
}
// Replace a Setting in the ini
// bool set_ini(string $old_filename, string $new_filename)
// $old_filename = The old log file name
// $new_filename = the new log file name
function set_ini($old_filename, $new_filename)
{
// Open the file for writing
$fp = fopen('feedback_server.ini', 'c+');
$canWrite = false;
//Waiting until file is locked for writing
while (!$canWrite)
{
$canWrite = flock($fp, LOCK_EX);
backoff();
}
$content = fread($fp, filesize('feedback_server.ini'));
$new_content = str_replace($old_filename, $new_filename, $content);
ftruncate($fp, 0);
fseek($fp, SEEK_SET);
fwrite($fp, $new_content);
fclose($fp);
}
// The function to convert ANY integers to hex
// We use it in the script to convert 64-bit integer to hex
// Input: Decimal string
// Output: Hex string
// ******** In this function we pad zeros in front of the result hex to represent the number as
64-bits
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;
// The ending condition
$flag = true;
while($flag)
{
$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)
{
// Calculate the next div
$div = $remainder == 0 ? $divisor[$pos] : $remainder.$divisor[$pos];
$remainder = $div % 16;
$quotient = $quotient.floor($div/16);
$pos++;
}
// Recast the divisor as string to make the $divisor[$pos] work
$quotient = trim_left_zeros($quotient);
$divisor = "$quotient";
$hexval = $hex[$remainder].$hexval;
// If the divisor is smaller than 15 then end the iteration
if (strlen($divisor)<=2)
{
if ($divisor<15)
{
$flag = false;
}
}
}
$hexval = $hex[$quotient].$hexval;
$hexval = trim_left_zeros($hexval);
// Pad zeros (only because we are using this function for 64-bit integers)
//$hexval = str_repeat('0', 16-strlen($hexval)).$hexval;
return $hexval;
}
// Trim the zeros at the left of a number
function trim_left_zeros($str)
{
$str = ltrim($str, '0');
if (empty($str))
{
$str = '0';
}
return $str;
}
?>
----
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/108794
Del: integrated -- https://master.php.net/note/delete/108794/integrated
Del: useless -- https://master.php.net/note/delete/108794/useless
Del: bad code -- https://master.php.net/note/delete/108794/bad+code
Del: spam -- https://master.php.net/note/delete/108794/spam
Del: non-english -- https://master.php.net/note/delete/108794/non-english
Del: in docs -- https://master.php.net/note/delete/108794/in+docs
Del: other reasons-- https://master.php.net/note/delete/108794
Reject -- https://master.php.net/note/reject/108794
Search -- https://master.php.net/manage/user-notes.php