Skip to content

Instantly share code, notes, and snippets.

@zined
Created September 23, 2012 15:35
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 zined/3772056 to your computer and use it in GitHub Desktop.
Save zined/3772056 to your computer and use it in GitHub Desktop.
padded, reversed, hex strings without nulls
#!/usr/bin/php
<?php
function ascii_hex ($char) {
$ret = '';
for ($i=0;$i<strlen($char);++$i)
$ret .= dechex(ord($char{$i}));
if (strlen($ret) < 2) return '0'.$ret;
return $ret;
}
function string_hex_nonull ($string) {
$string = strrev($string);
$len = strlen($string);
$packs = floor($len / 8);
$pad = 8 - $len % 8;
$hex_all = array();
$hex = '0x';
for ($i=0;$i<$pad;++$i)
$hex .= 'AA';
for ($i=0;$i<8-$pad;++$i)
$hex .= ascii_hex($string{$i});
array_push($hex_all, $hex);
for ($i=0;$i<$packs;++$i)
array_push($hex_all, '0x'. ascii_hex(substr($string, ($i+1)*8-$pad, 8)));
return $hex_all;
}
$orig = "some text without sense we want 'A' padded, reversed, and in hex!!\n";
$hex_all = string_hex_nonull($orig);
foreach (array_reverse($hex_all) as $hex) {
print $hex ."\t";
$hex = strrev(substr($hex, 2));
for ($i=0;$i<16;$i+=2) {
if ($hex{$i+1}.$hex{$i} == 'AA') break;
print chr(hexdec($hex{$i+1}.$hex{$i}));
}
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment