Jump to content

Recommended Posts

I want functions similar to base64_encode() and base64_decode() however only numbers and lowercase letters should be used.

 

I want to be able to convert a string of text or binary data into a series of numbers and lowercase letters.

 

My function works with numbers but not with text or binary data.

 

<?php

echo base36_encode('555555555');
echo '<hr />';
echo base36_encode('Hello World, this is a test!');

function base36_encode($base10){
    return base_convert($base10,10,36);
}

function base36_decode($base36){
    return base_convert($base36,36,10);
}

?>

base 36 isn't a multiple of 2 which complicates things.  It might be easier to use base 32, eg a-z and 0-5.  Then you can encode exactly 5 bits of data in each character.

 

Another alternative would be to start with base 64 encoding, and then do a conversion like this:

 

a => a

b => b

...

z => z

0 => 0

1 => 1

...

8 => 8

 

So everything from a-z and 0-8 is left unchanged, but the number 9 is reserved as a special character to indicate something from the other half of the base 64 character set.  That other half can be encoded as 9 followed by one of a-z and 0-8.  eg

 

9 => 99

A => 9a

B => 9b

C => 9c

...

 

What you'll end up with is an encoding using only 36 characters.

Much like you were told earlier today on DevShed, there isn't much reason to use your own encoding scheme, and it will just complicate what you're trying to do if you use a base that's not a power of 2.

 

-Dan

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.