I haven't seen this posted anywhere (not that I looked), so here it is! Ported by yours truely, from iago's BnetPacket.java.
/* Creates a hex representation of a string */
/* Ported from iago's JavaOp2 */
function debugOutput($buffer) {
$i = 0;
$j = 0;
$returnString = "";
for($i = 0; $i < strlen($buffer); $i++) {
if(($i != 0) && ($i % 16 == 0)) {
$returnString = $returnString . "\t";
for($j = $i - 16; $j < $i; $j++) {
if(ord($buffer[$j]) < 0x20 || ord($buffer[$j]) > 0x7F)
$returnString = $returnString . '.';
else
$returnString = $returnString . $buffer[$j];
}
// Add a linefeed after the string
$returnString = $returnString . "\n";
}
$returnString = $returnString . bin2hex($buffer[$i]) . " ";
}
if($i != 0 && $i % 16 != 0) {
for($j = 0; $j < ((16 - ($i % 16)) * 3); $j++) {
$returnString = $returnString . " ";
}
}
$returnString = $returnString . "\t";
if($i > 0 && ($i % 16) == 0) {
$j = $i - 16;
} else {
$j = ($i - ($i % 16));
}
for(; $i >= 0 && $j < $i; $j++) {
if(ord($buffer[$j]) < 0x20 || ord($buffer[$j]) > 0x7F) {
$returnString = $returnString . ".";
} else {
$returnString = $returnString . $buffer[$j];
}
}
$returnString = $returnString . "\n";
$returnString = $returnString . "Length: " . strlen($buffer) . "\n";
return "<pre>" . $returnString . "</pre>";
}