Created
September 23, 2012 15:35
-
-
Save zined/3772056 to your computer and use it in GitHub Desktop.
padded, reversed, hex strings without nulls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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