Ben Dodson

Freelance iOS, macOS, Apple Watch, and Apple TV Developer

Google extended encoding made easy!

I've been having a play around with Google Charts recently but came across a problem with the range of values that can be used. With simple encoding, you can only have values between 0 and 64, and with text encoding only values between 0 to 100. This is annoying when dealing with things such as tracking website hits or weight when values are typically much higher.

I played around with some functions to try and factorise the numbers so that they would be in the range of 0 to 100 but this didn't go so well so I decided to tackle Google's extended encoding.

This is a system of encoding that basically takes a pair of alphanumeric characters and translates them into a number between 0 and 4095 (much better!). So for example, B9 translates to 125 and .a would translate to 4058. However, although this has a much larger range, it's a bit harder to get your head around and so what is needed is a simple function (or two) to convert to and from the extended encoding. Predictably, I have said PHP functions here:

<?php

function array_to_extended_encoding($array)
{
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';

    $encoding = '';
    foreach ($array as $value) {
        $first = floor($value / 64);
        $second = $value % 64;
        $encoding .= $characters[$first] . $characters[$second];
    }

    return $encoding;
}

?>
<?php

function extended_encoding_to_array($string)
{
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';  

    for ($i=0;$i<strlen($characters);$i++) {
        $first[$characters[$i]] = $i * 64;
    }

    for ($i=0;$i<strlen($characters);$i++) {
        $second[$characters[$i]] = $i;
    }

    $pairs = str_split($string,2);
    foreach ($pairs as $pair) {
        $value = $first[$pair[0]];
        $value += $second[$pair[1]];
        $values[] = $value;
    }

    return $values;
}

?>

They work fairly simply by taking advantage of PHP's casting methods in that a string can be interpreted as an array.

In the first function, array_to_extended_encoding(), an array of numbers should be passed e.g. array(250,39,400,1904,2). The function first lists all of the extended encoding characters in order as one string named $characters. It then loops through our array of numbers, and creates two variables; $first and $second. In $first, we store the number from our array divided by 64 and rounded down using the floor() function. For $second, we use the modulus operator (%) to find the remainder once the number from the array is divided by 64. We then take both $first and $second and work out the encoding by looking up the numbers from within the $characters cast as an array. This gives us 2 characters which make up the extended encoding for the number. We keep extending the value of $encoding until we end up with a string representing the full extended encoding of the array passed to the function.

The second function, extended_encoding_to_array(), is slightly more advanced. It accepts a string as its only parameter which should be an extended encoding (e.g. $encoding = 'AA..B9aC'). We first list all of the characters in a string as we did for our previous function, but then we create two arrays which will contain all of the numbers we need to decode the extended encoding. In $first, we create an array of each character in the $characters string as a number multiplied by 64 (so the letter b would be 64 as it is signified by number 1 in the array multiplied by 64). Within $second we perform a similar operation but instead just assign the number of the character rather than multiplying it by 64. This gives us two arrays, with keys relating to each character that can be used as an encoding and values as the numerical equivalent. It is now a simple case of splitting the string that was passed to the function into chunks of 2 characters using str_split() and looping through the returned array setting the value of each character within the pair from the arrays we created. We then add the two returned numbers together to give us the decoded figure and add it to an array which will be returned by the function.

Simple? It has certainly made things a lot easier for the functions I'm writing for a PHP wrapper for Google Charts and hopefully it will help somebody else as well. If you have any questions or comments, please get in touch.

Firefox extensions updated for Beta 3 compatibility » « Firefox Extensions page will be right back!

Want to keep up to date? Sign up to my free newsletter which will give you exclusive updates on all of my projects along with early access to future apps.