Solved

PHP: Binary Safe  Base36 Encoding / Decoding

Posted on 2010-11-09
1
676 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
ID: 34097527
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Keep getting 503 on Curl request 6 33
Access Object's Property 9 22
phpmailer in WHILE loop - weird results 10 29
WordPress website error - Unable to access website. 5 35
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

863 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

22 Experts available now in Live!

Get 1:1 Help Now