Valhalla Legends Forums Archive | General Programming | Code Submission --- Updated DebugOutput function

AuthorMessageTime
Grok
This version of DebugOutput shows the offset like you'd see when using the debug command:

[code]
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
[/code]

Sample Usage:
[code]
? 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.........
[/code]

Hope this helps.
Grok
December 23, 2002, 10:17 PM
Noodlez
thanks++ :)
December 23, 2002, 10:21 PM
dRAgoN
Nice format looks good Grok.  ;)

~l)ragon
December 23, 2002, 11:41 PM
Eibro
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:

[code]
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();
}
[/code]
December 24, 2002, 1:25 AM

Search