Solved

PHP: Binary Safe  Base36 Encoding / Decoding

Posted on 2010-11-09
1
715 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

705 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