note 34510 modified in function.base-convert by vrana

From: Date: Tue, 17 Aug 2004 14:52:30 +0000
Subject: note 34510 modified in function.base-convert by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80094@lists.php.net to get a copy of this message
I wrote a function for converting numbers not only into 0-9 and a-z: ----- <?php function custombase_convert($numstring, $baseFrom = "0123456789", $baseTo = "0123456789") { $numstring = (string) $numstring; $baseFromLen = strlen($baseFrom); $baseToLen = strlen($baseTo); if ($baseFrom == "0123456789") // No analyzing needed, because $numstring is already decimal { $decVal = (int) $numstring; } else { $decVal = 0; for ($len = (strlen($numstring) - 1); $len >= 0; $len--) { $char = substr($numstring, 0, 1); $pos = strpos($baseFrom, $char); if ($pos !== FALSE) { $decVal += $pos * ($len > 0 ? pow($baseFromLen, $len) : 1); } $numstring = substr($numstring, 1); } } if ($baseTo == "0123456789") // No converting needed, because $numstring needs to be converted to decimal { $numstring = (string) $decVal; } else { $numstring = FALSE; $nslen = 0; $pos = 1; while ($decVal > 0) { $valPerChar = pow($baseToLen, $pos); $curChar = floor($decVal / $valPerChar); if ($curChar >= $baseToLen) { $pos++; } else { $decVal -= ($curChar * $valPerChar); if ($numstring === FALSE) { $numstring = str_repeat($baseTo{1}, $pos); $nslen = $pos; } $numstring = substr($numstring, 0, ($nslen - $pos)) . $baseTo{$curChar} . substr($numstring, (($nslen - $pos) + 1)); $pos--; } } if ($numstring === FALSE) $numstring = $baseTo{1}; } return $numstring; } ?> ------ The function arguments: $numstring - String with number to convert. (e.g. "15" or "5F") $baseFrom - Chars of the base the number is in. (e.g. "0123456789" for decimal or "01" for binary) $baseTo - Chars of the base to convert the number. (e.g. "0123456789" for decimal or "01" for binary) I wrote it for writing numbers in files, and so I convert between "0123456789" (decimal) and "\x01\x02\x03...\xFF" - uses very low disc space :-) (You could of course also add the null char (\x00), but >I< need it to seperate the numbers) --was-- I wrote a function for converting numbers not only into 0-9 and a-z: ----- function custombase_convert($numstring, $baseFrom = "0123456789", $baseTo = "0123456789") { $numstring = (string) $numstring; $baseFromLen = strlen($baseFrom); $baseToLen = strlen($baseTo); if ($baseFrom == "0123456789") // No analyzing needed, because $numstring is already decimal { $decVal = (int) $numstring; } else { $decVal = 0; for ($len = (strlen($numstring) - 1); $len >= 0; $len--) { $char = substr($numstring, 0, 1); $pos = strpos($baseFrom, $char); if ($pos !== FALSE) { $decVal += $pos * ($len > 0 ? pow($baseFromLen, $len) : 1); } $numstring = substr($numstring, 1); } } if ($baseTo == "0123456789") // No converting needed, because $numstring needs to be converted to decimal { $numstring = (string) $decVal; } else { $numstring = FALSE; $nslen = 0; $pos = 1; while ($decVal > 0) { $valPerChar = pow($baseToLen, $pos); $curChar = floor($decVal / $valPerChar); if ($curChar >= $baseToLen) { $pos++; } else { $decVal -= ($curChar * $valPerChar); if ($numstring === FALSE) { $numstring = str_repeat($baseTo{1}, $pos); $nslen = $pos; } $numstring = substr($numstring, 0, ($nslen - $pos)) . $baseTo{$curChar} . substr($numstring, (($nslen - $pos) + 1)); $pos--; } } if ($numstring === FALSE) $numstring = $baseTo{1}; } return $numstring; } ------ The function arguments: $numstring - String with number to convert. (e.g. "15" or "5F") $baseFrom - Chars of the base the number is in. (e.g. "0123456789" for decimal or "01" for binary) $baseTo - Chars of the base to convert the number. (e.g. "0123456789" for decimal or "01" for binary) I wrote it for writing numbers in files, and so I convert between "0123456789" (decimal) and "\x01\x02\x03...\xFF" - uses very low disc space :-) (You could of course also add the null char (\x00), but >I< need it to seperate the numbers) http://php.net/manual/en/function.base-convert.php

« previous php.notes (#80094) next »