Skip to content

Instantly share code, notes, and snippets.

@0xa
Created August 19, 2012 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xa/3397353 to your computer and use it in GitHub Desktop.
Save 0xa/3397353 to your computer and use it in GitHub Desktop.
<?php
function encode($data) {
global $format;
array_unshift($data, $format);
$raw = call_user_func_array("pack", $data);
$encoded = bin2hex($raw);
return $encoded;
}
function decode($encoded) {
global $format;
$raw = hex2bin($encoded);
return unpack($format, $raw);
}
$format = "LLc*";
$enc = encode(array(
42,
1337,
"woot"
));
$dec = decode($enc);
echo "<pre>";
echo "Encoded: $enc\n";
echo "Decoded: $dec[0]; $dec[3]; $dec[3] \n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment