Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a third party system that submits what I am assuming is a byte array data from OBDII port on our fleet vehicles.

I was getting values like:

  • AA== (bool)
  • AQ== (bool)
  • AAAaMA== (int)
  • AAA0IQ== (int)
  • Mjow (string)
  • UzozLTIwNA== (string)

I know what the type is for each field but the regular base64_decode() function wouldn't decode them and neither did the online Base64 decoders.

Here is what I came up with that works, I just curious if this is the right way to decode base64 encoded bytes.

It essentially takes the input down to binary and converts the binary to the type. It works as expected but I feel like I'm over complicating it.

class Decoder
{
    /**
     * Decodes Base64 ByteArray to Number
     *
     * @param string $encoded
     * @return number
     */
    public static function getNumber($encoded)
    {
        $decoded = base64_decode($encoded);

        $byteArray = array();

        foreach(str_split($decoded) as $byte)
        {
            $byteArray[] = sprintf("%08b", ord($byte));
        }

        $raw_bytes = implode(' ', $byteArray);

        return bindec($raw_bytes);
    }

    /**
     * Decodes Base64 ByteArray to ASCII String
     *
     * @param string $encoded
     * @return string
     */
    public static function getString($encoded)
    {
        $decoded = base64_decode($encoded);

        $raw = "";

        foreach(str_split($decoded) as $byte)
        {
            $raw .= chr(bindec(sprintf("%08b", ord($byte))));
        }

        return $raw;
    }

    /**
     * Decodes Base64 ByteArray to Boolean
     *
     * CHEATER FUNCTION
     *
     * Simply interprets Base64 value against known true/false values
     *
     * Value: "AQ==" will return true everything else will be false
     *
     * @param $encoded
     * @return bool
     */
    public static function getBool($encoded)
    {
        return ($encoded == "AQ==");
    }

    /**
     * Decodes Base64 ByteArray to Boolean
     *
     * Actually does decode and comparison on byte level.
     *
     * More expensive but is a true decode.
     *
     * @param $encoded
     * @return bool
     */
    public static function zDecodeBool($encoded)
    {
        $decoded = base64_decode($encoded);

        $byteArray = array();

        foreach(str_split($decoded) as $byte)
        {
            $byteArray[] = sprintf("%08b", ord($byte));
        }

        return $byteArray[0] == "00000001";
    }
}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

Your code is generally clean, you have accurate comments & variable naming is mostly consistent.

$raw_bytes and $byteArray use two different types of naming. It's best if you choose one style, and stick with it.

With return ($encoded == "AQ==") and return $byteArray[0] == "00000001", you don't have a consistent use of brackets, it's best to stick to one.


Onto code, in getNumber(), you initialise an array, build to it, and then proceed to implode it.
However, in getString(), you initialise a string, build to it, and then return it.

In getNumber(), it'd be better if you just built to the string instead of an array.

$byteArray = array();
foreach(str_split($decoded) as $byte)
{
    $byteArray[] = sprintf("%08b", ord($byte));
}
$raw_bytes = implode(' ', $byteArray);
return bindec($raw_bytes);

into:

$rawBytes = "";
foreach(str_split($decoded) as $byte)
{
    $rawBytes .= ' ' . sprintf("%08b", ord($byte));
}
return bindec($rawBytes);

Also, zDecodeBool() (could have a better name), runs nearly the same code as getString(), you could consider a compromise between the two functions.


Other than that, your code looks nice and clean.

share|improve this answer
1  
Thanks, I made the changes to the formatting you suggested –  McGee.WIll Jun 11 at 13:58
    
Thanks I made the changes you suggested for format. Being the only programmer where I work its tough to catch those little things. zDecodeBool was actually just an after thought and I removed it anyway as I thinks its unnecessary. –  McGee.WIll Jun 11 at 14:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.