HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

PHP Base64 ByteArray Decoder

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpbytearraydecoderbase64

Problem

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
*

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#93274, answer score: 6

Revisions (0)

No revisions yet.