note 94400 modified in function.dechex by felipe

From: Date: Sat, 07 Nov 2009 14:48:30 +0000
Subject: note 94400 modified in function.dechex by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162483@lists.php.net to get a copy of this message
Thanks to jbleau at gmail dot com for great script. But but it does not return correct results, because Modulus for example 78559093562500%16 returns -12, but not 4 (i'll post about this bug too). But for now the working script is: <?php function dec_to_hex($dec) { $sign = $h = ""; // suppress errors. '$h' is added by Mozg if( $dec < 0){ $sign = "-"; $dec = abs($dec); } $hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f' ); do { $mozg = $dec%16; if ($mozg < 0) $mozg += 16; // Checking if Modulus is positive? and corrects its "inertia". added by Mozg $h = $hex[$mozg] . $h; $dec /= 16; } while( $dec >= 1 ); return $sign . $h; } ?> --was-- Thanks to jbleau at gmail dot com for great script. But but it does not return correct results, because Modulus for example 78559093562500%16 returns -12, but not 4 (i'll post about this bug too). But for now the working script is: function dec_to_hex($dec) { $sign = $h = ""; // suppress errors. '$h' is added by Mozg if( $dec < 0){ $sign = "-"; $dec = abs($dec); } $hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f' ); do { $mozg = $dec%16; if ($mozg < 0) $mozg += 16; // Checking if Modulus is positive? and corrects its "inertia". added by Mozg $h = $hex[$mozg] . $h; $dec /= 16; } while( $dec >= 1 ); return $sign . $h; } ?> http://php.net/manual/en/function.dechex.php

« previous php.notes (#162483) next »