Valhalla Legends Archive

Programming => General Programming => Topic started by: Grok on December 23, 2002, 04:17 PM

Title: Code Submission --- Updated DebugOutput function
Post by: Grok on December 23, 2002, 04:17 PM
This version of DebugOutput shows the offset like you'd see when using the debug command:

Public Function DebugOutput(ByVal sIn As String) As String
    Dim x1 As Long, y1 As Long
    Dim iLen As Long, iPos As Long
    Dim sB As String, sT As String
    Dim sOut As String
    Dim Offset As Long, sOffset As String
    
    'build random string to display
'    y1 = 256
'    sIn = String(y1, 0)
'    For x1 = 1 To 256
'        Mid(sIn, x1, 1) = Chr(x1 - 1)
'        'Mid(sIn, x1, 1) = Chr(255 * Rnd())
'    Next x1
    
    iLen = Len(sIn)
    If iLen = 0 Then Exit Function
    sOut = ""
    Offset = 0
    For x1 = 0 To ((iLen - 1) \ 16)
        sOffset = Right$("0000" & Hex(Offset), 4)
        sB = String(48, " ")
        sT = "................"
        For y1 = 1 To 16
            iPos = 16 * x1 + y1
            If iPos > iLen Then Exit For
            Mid(sB, 3 * (y1 - 1) + 1, 2) = Right("00" & Hex(Asc(Mid(sIn, iPos, 1))), 2) & " "
            Select Case Asc(Mid(sIn, iPos, 1))
            Case 0, 9, 10, 13
            Case Else
                Mid(sT, y1, 1) = Mid(sIn, iPos, 1)
            End Select
        Next y1
        If Len(sOut) > 0 Then sOut = sOut & vbCrLf
        sOut = sOut & sOffset & ":  "
        sOut = sOut & sB & "  " & sT
        Offset = Offset + 16
    Next x1
    DebugOutput = sOut
End Function

Sample Usage:
? debugoutput("209348u20r8ur023ru02ru8")
0000:  32 30 39 33 34 38 75 32 30 72 38 75 72 30 32 33   209348u20r8ur023
0010:  72 75 30 32 72 75 38                              ru02ru8.........

Hope this helps.
Grok
Title: Re: Code Submission --- Updated DebugOutput functi
Post by: Noodlez on December 23, 2002, 04:21 PM
thanks++ :)
Title: Re: Code Submission --- Updated DebugOutput functi
Post by: l)ragon on December 23, 2002, 05:41 PM
Nice format looks good Grok.  ;)

~l)ragon
Title: Re: Code Submission --- Updated DebugOutput functi
Post by: Eibro on December 23, 2002, 07:25 PM
I created a simple hex viewer using some functions similar to that. I see yours is much more optimized than mine was... I made a few changes to it and now we have a similar function in C++. I hope you don't mind me posting it:

std::string DebugOutput(const std::string &in)
{
      using namespace std;
      typedef string::size_type st;
  
      stringstream buf;
      st length = in.length();
  
      for (st ln = 0; ln <= length/16; ++ln)
      {
            buf << setfill('0') << setw(6) << hex << ln*16 << ": ";
            for (int i = 0; i < 16; ++i)
            {
                  if (ln*16+i < length)
                        buf << setfill('0') << setw(2) << hex << uppercase
                        << static_cast<int>(in[ln*16+i]) << ' ';
                     else
                              buf << "00" << ' ';
                  }
        
               buf << ' ';

            for (i = 0; i < 16; ++i)
                  {
                     if (ln*16+i < length)
                              buf << (isprint(in[ln*16+i]) ? in[ln*16+i] : '.');
                     else
                              buf << '.';
                  }
            buf << '\n';
         }
         return buf.str();
}