Solved

PHP: Binary Safe  Base36 Encoding / Decoding

Posted on 2010-11-09
1
669 Views
Last Modified: 2012-05-10
I want to be able to convert a string of text or binary data into a series of numbers and lowercase letters.

This should work in a way similar to base64_encode() however only numbers and lowercase letters should be used.

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);
}

?>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
Comment Utility
Base64 encoding uses 6 bits ( 2**6  = 64 ) to package data. So if you take (say) 3 characters at 8 bits each that is 24 bits and is equivalent to four sets of 6 bits. Thus if you envisage the data as being laid as a stream of bits and picked up in groups of 6 then you get the idea (this is more how uuencode worked but you get the idea)

So your problem with base 36 is that 36 is 2**5.17 which will never do. You could use base 32 which is exactly five bits and then group your data into 40 bit groups (5 eight-bit bytes == 8 five bit groups) then you would be in business, so to speak.

The real problem here is getting the data to map into consistent groups and patterns so that you can use bit shifting and masking to pull out the data streams

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now