note 34245 added to function.dechex

From: Date: Sat, 19 Jul 2003 15:53:32 +0000
Subject: note 34245 added to function.dechex
Groups: php.notes 
Request: Send a blank email to php-notes+get-52387@lists.php.net to get a copy of this message
now, here is a nice and small function to convert integers to hex strings and it avoids use of the DECHEX funtion because that function changed it's behavior too often in the past (now, in PHP version 4.3.2 it works with numbers bigger than 0x7FFFFFFF correctly, but i need to be backward compatible). function &formatIntegerForOutput($value) { $text = "00000000"; $transString = "0123456789ABCDEF"; // handle highest nibble (nibble 7): $nibble = $value & 0x70000000; $nibble >>= 28; if ($value < 0) { $nibble = $nibble | 0x00000008; } $text[0] = $transString[$nibble]; $value &= 0x0FFFFFFF; // nibbles 0 to 6: for ($a = 7; $a > 0; $a --) { $nibble = $value & 0x0000000F; $text[$a] = $transString[$nibble]; $value >>= 4; } return $text } this function should be not too slow and is really simple. I don't know, if the DECHEX function in the future will pad it's output to ever be 8 characters in length - so for backward compatibility reasons even in future PHP versions i avoided to use it. ---- Manual Page -- http://www.php.net/manual/en/function.dechex.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34245 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34245&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34245&report=yes

« previous php.notes (#52387) next »