[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 756
  • Last Modified:

PHP: Binary Safe Base36 Encoding / Decoding

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
hankknight
Asked:
hankknight
1 Solution
 
Beverley PortlockCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now