Question

Okay, so I am having a bit of trouble (likely due to my ignorance) figuring this out.

$binary = base64_decode("AAAAAgYA3ncsRjy9");

So, what I have here are 12 bytes of binary data, yes?

I need to turn this into:

$binary = array("0"=>"00000000", "1"=>"00000000", "2"=>"00000000", "3"=>"00000010", "4"=>"00000110", "5"=>"00000000", "6"=>"11011110", "7"=>"01110111, "8"=>"00101100", "9"=>"01000110", "10"=>"00111100", "11"=>"10111101");

And, ultimately, drop the first 6 bytes, and split the rest into 2 byte pairs.

$binary = array("0"=>"1101111001110111", "1"=>"0010110001000110", "2"=>"0011110010111101"

Then, convert those 2 bytes chunks into base 10 decimal.

$binary = array("0"=>"56951", "1"=>"11334", "2"=>"15549");

The reason I have chose to include the actual data is because I want to make sure I am using the correct terminology. I have been trying to work with pack/unpack and have simply not been able to get this done, likely due to my not understanding the different available choices for the format argument.

I am certain I have been extremely confusing in my explanation, please accept my apologies. Any help would be greatly appreciated.


EDIT:

For posterity:

PHP Manual - function.pack

PHP Manual - function.unpack

PHP Manual - function.call-user-func-array

Was it helpful?

Solution

This should do it:

$binary = base64_decode("AAAAAgYA3ncsRjy9");
$binary = substr($binary, 6);
var_dump(unpack("n*", $binary));

n is unsigned short (always 16 bit, big endian byte order). With * this format is used for the entire remaining data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top