By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

php array help

P: 11

I want to use an array tu transform from base 64 to decimal.
The problem is the function always returns 0... :(
The reason is that $digit64[$str[$i++]] is always 0 but i don't know why.
$str[$i++] is 'B' just as it should but $digit64[$str[$i++]] is 0;
if i try with $digit64['B'] the result is 11 (11 is the corect result);
Please help.
Thx in advance

Expand|Select|Wrap|Line Numbers
  1. $digit64 = array('0'=>0, '1'=>1, '2'=>2, '3'=>3, '4'=>4, '5'=>5, '6'=>6, '7'=>7, '8'=>8, '9'=>9, 'A'=>10, 'B'=>11, 'C'=>12, 'D'=>13, 'E'=>14, 'F'=>15, 'G'=>16, 'H'=>17, 'I'=>18, 'J'=>19, 'K'=>20, 'L'=>21, 'M'=>22, 'N'=>23, 'O'=>24, 'P'=>25, 'Q'=>26, 'R'=>27, 'S'=>28, 'T'=>29, 'U'=>30, 'V'=>31, 'W'=>32, 'X'=>33, 'Y'=>34, 'Z'=>35, 'a'=>36, 'b'=>37, 'c'=>38, 'd'=>39, 'e'=>40, 'f'=>41, 'g'=>42, 'h'=>43, 'i'=>44, 'j'=>45, 'k'=>46, 'l'=>47, 'm'=>48, 'n'=>49, 'o'=>50, 'p'=>51, 'q'=>52, 'r'=>53, 's'=>54, 't'=>55, 'u'=>56, 'v'=>57, 'w'=>58, 'x'=>59, 'y'=>60, 'z'=>61, 'z'=>61, '+'=>62, '-'=>63);
  3. function base10($nr){
  4.     $i = 0;
  5.     $str = $nr;
  6.     $nr10 = 0;
  7.     $n = strlen($str);
  8.     while($i<$n){
  9.         $nr10 = $nr10*64 + $digit64[$str[$i++]];
  10.     }
  11.     return $nr10;
  12. }
  14. echo base10('B');
Sep 11 '08 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,639
maybe you like this function base64_decode()

Sep 11 '08 #2

Expert 5K+
P: 5,058

That would be because your array, $digit64, is defined outside the scope of the function where it is used. As a result, the array is undefined inside the function, causing it to return a NULL value when used, which is converted into 0 when you use it in your calculations.

Try defining the array inside the function or import it using the global keyword.
Sep 11 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.