473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

php array help

11
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


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);
  2.  
  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. }
  13.  
  14. echo base10('B');
  15.  
Sep 11 '08 #1
2 1106
Dormilich
8,658 Expert Mod 8TB
maybe you like this function base64_decode()

regards
Sep 11 '08 #2
Atli
5,058 Expert 4TB
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.
Sep 11 '08 #3

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

Similar topics

8
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet =>...
2
by: Antti Nummiaho | last post by:
Consider the following javascript: var temp = new Array(new Array(0)) document.writeln(temp) temp = new Array(new Array(0,1)) document.writeln(temp) One would assume that it would print "0...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.