Convert Largest no: of Base 36 (zzz..zzzz) to Binary

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Convert Largest no: of Base 36 (zzz..zzzz) to Binary

Post by anjanesh »

Im having a huge list of variables which can either have the value 0 or 1 (for yes or no). I can use arrays too but imagine the list is like some 1000. So I thought instead of having an array of 1000 elements I can have just 1 variable which is a base n representation of the binary form (100111010..001101001..upto 1000 places) and then I can use base_convert to converty it back to binary and do my needful search etc.

string base_convert ( string number, int frombase, int tobase) can take base values from 2 to 36. 36 is the highest (z) So I wanted to know the largest possible binary value for this (Yes this can be done mathematically too) and tried :

Code: Select all

<?php
$data="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; // 36 z's - largest number
$dest=base_convert($data,36,2); // Binary representation
echo $dest;
?>
This just displays 64 0's. I tried 35z's and 1 y and just 35 z's too but same output.
On the other hand if I convert it dec or hex it shows up.
Is it the memory limitation of 64 bytes ? I was hoping string would handle more.
Any ideas ?
Last edited by anjanesh on Sun Aug 07, 2005 2:49 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

64-bits is the largest integer php can store without help. Each of those digits in a base 36 would take up 6 bits.

Code: Select all

&lt;?php

$test = 'zzzzzzzzzz'; // 10 z's

echo base_convert($test,36,2);
echo "\n".base_convert($test.'z',36,2); // add 1 more z

?&gt;
outputs

Code: Select all

1100111111010100000110111001000011111111111111111111
111010011100111010011111000000110010000000000000000000000
notice how the returned string explodes into integer overrun..


instead, I'd suggest storing off 8 of these flags, and using [php_man]chr[/php_man]() to convert their base_converted (2,10) value to a character, which you can then concatenate to a string..
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

You mentioned abt the limitation of int. But what abt the limitation of string. I thought it could store huge values. After all base_convert is returning a string not int value.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, base convert returns a string, but it functions off of numbers and numbers only.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I can get the actual value for converting from base 2 to base 16 or base 32 or base 2^n. But this involves chopping up the binary value into n bits each where n is the number of bits required for the detination base.
For example : for base 16 its 4 (1111) so the original binary value is split into 4 bits each. For base 32 its 5 and so on.

Code: Select all

$data = '11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111';
$DestByteLength=4;
$DestBase=16;
$data = str_repeat('0',(ceil(strlen($data)/$DestByteLength)-(strlen($data)/$DestByteLength))*$DestByteLength).$data;
$dest=base_convert($data, 2, $DestBase);
echo 
'Binary Value : '.$data.
'<br>Binary Length : '.strlen($data).
'<br>Dest Base Value : '.$dest.
'<br>Dest Base Length : '.strlen($dest).
'<br>Actual Dest Value : ';
$ActualDestValue="";
for ($i=0;$i<strlen($data);$i+=$DestByteLength)
 $ActualDestValue.=base_convert(substr($data,$i,$DestByteLength), 2, $DestBase);
echo $ActualDestValue;
Post Reply