note 108794 modified in function.dechex by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 May 2012 15:24:00 +0000 |
| Subject: | note 108794 modified in function.dechex by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188237@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. [It will handle] 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. <?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; 		$hexva
l = 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; 	} ?>
--was--
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;
}
?>
http://php.net/manual/en/function.dechex.php