php array help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gulyan
    New Member
    • Aug 2008
    • 11

    php array help

    Hy,

    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


    Code:
    $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);
    
    function base10($nr){
    	$i = 0;
    	$str = $nr;
    	$nr10 = 0;
    	$n = strlen($str);
    	while($i<$n){
    		$nr10 = $nr10*64 + $digit64[$str[$i++]];
    	}
    	return $nr10;
    }
    
    echo base10('B');
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    maybe you like this function base64_decode()

    regards

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      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.

      Comment

      Working...