Author Topic: [PHP] DebugOutput  (Read 3236 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[PHP] DebugOutput
« on: December 14, 2005, 07:48:39 pm »
I haven't seen this posted anywhere (not that I looked), so here it is! Ported by yours truely, from iago's BnetPacket.java.

Code: [Select]
  /* 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>";
  }
I'd personally do as Joe suggests

You might be right about that, Joe.