note 25337 deleted from ref.bc by aidan
| From: | aidan@php.net | Date: | Sat, 11 Sep 2004 12:59:38 +0000 |
| Subject: | note 25337 deleted from ref.bc by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76480@lists.php.net to get a copy of this message | ||
Note Submitter: pulstar at mail dot com
----
Here some examples of the previous post:
a) Converting bases:
Converting from decimal to binary:
$x=192;
$y=dec2base($x,2);
From binary to decimal:
$x=1100101001;
$y=base2dec($x,2);
Decimal to octal:
$x=192;
$y=dec2base($x,8);
Octal to decimal:
$x=18107362;
$y=base2dec($x,8);
Decimal to hexadecimal:
$x=139245;
$y=dec2base($x,16);
Hexadecimal to decimal:
$x="e0f13d";
$y=base2dec($x,16);
// note: you must use smallcaps unless you specify another digits. Example:
// $x="E0F13D";
// $y=base2dec($x,16,"0123456789ABCDEF");
Decimal to base32:
$x=92109743;
$y=dec2base($x,32);
Decimal to base64:
$x=92109743;
$y=dec2base($x,64);
Decimal to base256:
$x=92109743;
$y=dec2base($x,256);
As you can see, you can use any value from 2 to 256 as a valid base. The size of the number ($x)
doesn't matter, because BCMath is very powerful.
You can use these functions to convert a string to a value that can be passed by an URL. Example:
$string="*Here Anything+Exactly-As#You$Want@, using&any%characters(";
$decimal=base2dec($string,256);
$base64=dec2base($decimal,64);
<a href="script.php?var=<?php echo $base64 ?>">Click me</a>
You can pass the content of the $base64 var by an URL. It only will have letters, numbers, the
"_" and "-". After you get it from the URL, you must do the inverse way to get
the original string again:
$decimal=base2dec($var,64);
$string=dec2base($decimal,256);
echo $string;
b) "Compressing" data:
Converting an e-mail address to another base:
$digits="@0123456789abcdefghijklmnopqrstuvwxyz-.";
// these are the 39 characters used by e-mail addresses
$email="this.is@my-email.com";
$base64=decbase(basedec($email,64),39,$digits);
$base256=decbase(basedec($email,256),39,$digits);
$restored_email=decbase(basedec($base64,39,$digits),64);
$restored_email2=decbase(basedec($base256,39,$digits),256);
// $base64 is the equivalent of the email address in a base64 value, which can be passed by an URL
and is shorter than the original e-mail address in ASCII.
// $base256 is the same in a base256 value, which can be stored in a database and is even shorter
than $base64.
By this method you can compress informations that do not use the all 256 characters, such as names,
addresses, etc. You must figure which characters these informations will use and use it as your
digits.
To compress and "encrypt" you only need to scramble the digits used.
As these functions can be used for some kind of encryption too, you only need to use another sort of
digits, scrambled any way you want. Just remember to use the same sequence of digits for
reconversion, or you will get another thing. Here an example:
$digits="z6QgkmR1ZP irK9Ws?lbNCjnYwht.vyaJx5IM78-fODdUFHucpoS,2GEX403eALVTBq"; // 67
characters that can be used in names, for example
$data="This is my string. Can you See, what I want to do?";
$encrypted=decbase(basedec($data,256),67,$digits);
$decrypted=decbase(basedec($encrypted,67,$digits),256);
Well, I didn't tested my examples above, but the functions works for sure (I did spend a whole
night working on it)... :-)