note 108794 modified in function.dechex by danbrown

From: 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&#39;s input is a valid number thus I did NOT include any input verifying mechanism. So you have to do the verifying part yourself.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;&#9;// ==================== Support functions ====================&#13;&#10;&#9;// The backoff function during contention&#13;&#10;&#9;function backoff()&#13;&#10;&#9;{&#13;&#10;&#9;&#9;$miliSeconds = rand(0, 100);&#13;&#10;&#9;&#9;//Sleep for 0 - 100 miliseconds, to avoid collision&#13;&#10;&#9;&#9;usleep(round($miliSeconds*1000));&#13;&#10;&#9;}&#13;&#10;&#9;&#13;&#10;&#9;// Replace a Setting in the ini&#13;&#10;&#9;// bool set_ini(string $old_filename, string $new_filename)&#13;&#10;&#9;// $old_filename = The old log file name&#13;&#10;&#9;// $new_filename = the new log file name&#13;&#10;&#9;function set_ini($old_filename, $new_filename)&#13;&#10;&#9;{&#13;&#10;&#9;&#9;// Open the file for writing&#13;&#10;&#9;&#9;$fp = fopen(&#39;feedback_server.ini&#39;, &#39;c+&#39;);&#13;&#10;&#9;&#9;$canWrite = false;&#13;&#10;&#9;&#9;//Waiting until file is locked for writing &#13;&#10;&#9;&#9;while (!$canWrite)&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;$canWrite = flock($fp, LOCK_EX);&#13;&#10;&#9;&#9;&#9;backoff();&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;$content = fread($fp, filesize(&#39;feedback_server.ini&#39;));&#13;&#10;&#9;&#9;$new_content = str_replace($old_filename, $new_filename, $content);&#13;&#10;&#9;&#9;ftruncate($fp, 0);&#13;&#10;&#9;&#9;fseek($fp, SEEK_SET);&#13;&#10;&#9;&#9;fwrite($fp, $new_content);&#13;&#10;&#9;&#9;fclose($fp);&#13;&#10;&#9;}&#13;&#10;&#9;// The function to convert ANY integers to hex&#13;&#10;&#9;// We use it in the script to convert 64-bit integer to hex&#13;&#10;&#9;// Input: Decimal string&#13;&#10;&#9;// Output: Hex string&#13;&#10;&#9;// ******** In this function we pad zeros in front of the result hex to represent the number as 64-bits&#13;&#10;&#9;function dec2hex($str)&#13;&#10;&#9;{&#13;&#10;&#9;&#9;$hex = array(&#39;0&#39;,&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;,&#39;6&#39;,&#39;7&#39;,&#39;8&#39;,&#39;9&#39;,&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;D&#39;,&#39;E&#39;,&#39;F&#39;);&#13;&#10;&#9;&#9;// Result value&#13;&#10;&#9;&#9;$hexval = &#39;&#39;;&#13;&#10;&#9;&#9;// The quotient of each division operation&#13;&#10;&#9;&#9;$quotient = $str;&#13;&#10;&#9;&#9;$divisor = $str;&#13;&#10;&#9;&#9;// The ending condition&#13;&#10;&#9;&#9;$flag = true;&#13;&#10;&#9;&#9;while($flag)&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;$len = strlen($divisor);&#13;&#10;&#9;&#9;&#9;$pos = 1;&#13;&#10;&#9;&#9;&#9;$quotient = 0;&#13;&#10;&#9;&#9;&#9;// Take the first two digits as temp divisor and advance by 1 each iteration&#13;&#10;&#9;&#9;&#9;$div = substr($divisor, 0, 2);&#13;&#10;&#9;&#9;&#9;$remainder = $div[0];&#13;&#10;&#9;&#9;&#9;while($pos &#60; $len)&#13;&#10;&#9;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;&#9;// Calculate the next div&#13;&#10;&#9;&#9;&#9;&#9;$div = $remainder == 0 ? $divisor[$pos] : $remainder.$divisor[$pos];&#13;&#10;&#9;&#9;&#9;&#9;$remainder = $div % 16;&#13;&#10;&#9;&#9;&#9;&#9;$quotient = $quotient.floor($div/16);&#13;&#10;&#9;&#9;&#9;&#9;$pos++;&#13;&#10;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;&#9;// Recast the divisor as string to make the $divisor[$pos] work&#13;&#10;&#9;&#9;&#9;$quotient = trim_left_zeros($quotient);&#13;&#10;&#9;&#9;&#9;$divisor = &#34;$quotient&#34;;&#13;&#10;&#9;&#9;&#9;$hexval = $hex[$remainder].$hexval;&#13;&#10;&#9;&#9;&#9;// If the divisor is smaller than 15 then end the iteration&#13;&#10;&#9;&#9;&#9;if (strlen($divisor)&#60;=2)&#13;&#10;&#9;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;&#9;if ($divisor&#60;15)&#13;&#10;&#9;&#9;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;&#9;&#9;$flag = false;&#13;&#10;&#9;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;&#9;}&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;$hexval = $hex[$quotient].$hexval;&#13;&#10;&#9;&#9;$hexva l = trim_left_zeros($hexval);&#13;&#10;&#9;&#9;// Pad zeros (only because we are using this function for 64-bit integers)&#13;&#10;&#9;&#9;//$hexval = str_repeat(&#39;0&#39;, 16-strlen($hexval)).$hexval;&#13;&#10;&#9;&#9;return $hexval;&#13;&#10;&#9;}&#13;&#10;&#9;// Trim the zeros at the left of a number&#13;&#10;&#9;function trim_left_zeros($str)&#13;&#10;&#9;{&#13;&#10;&#9;&#9;$str = ltrim($str, &#39;0&#39;);&#13;&#10;&#9;&#9;if (empty($str))&#13;&#10;&#9;&#9;{&#13;&#10;&#9;&#9;&#9;$str = &#39;0&#39;;&#13;&#10;&#9;&#9;}&#13;&#10;&#9;&#9;return $str;&#13;&#10;&#9;}&#13;&#10;?&#62; --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

« previous php.notes (#188237) next »