HexDumper functions


Author: Dave
Date: 08.20.10 - 6:34am

I always have to search for these.

Function Hexdump(ByVal str, Optional hexOnly = 0) As String
    Dim s() As String, chars As String, tmp As String
    On Error Resume Next
    Dim ary() As Byte
    Dim offset As Long
    
    offset = 0
    str = " " & str
    ary = StrConv(str, vbFromUnicode)
    
    chars = "   "
    For i = 1 To UBound(ary)
        tt = Hex(ary(i))
        If Len(tt) = 1 Then tt = "0" & tt
        tmp = tmp & tt & " "
        x = ary(i)
        chars = chars & IIf((x > 32 And x < 127), Chr(x), ".")
        If i > 1 And i Mod 16 = 0 Then
            h = Hex(offset)
            While Len(h) < 6: h = "0" & h: Wend
            If hexOnly = 0 Then
                push s, h & "   " & tmp & chars
            Else
                push s, tmp
            End If
            offset = offset + 16
            tmp = Empty
            chars = "   "
        End If
    Next
    
    If tmp <> Empty Then
        If hexOnly = 0 Then
            h = Hex(offset)
            While Len(h) < 6: h = "0" & h: Wend
            h = h & "   " & tmp
            While Len(h) <= 56: h = h & " ": Wend
            push s, h & chars
        Else
            push s, tmp
        End If
    End If
    
    Hexdump = Join(s, vbCrLf)
    
    If hexOnly <> 0 Then
        Hexdump = Replace(Hexdump, " ", "")
        Hexdump = Replace(Hexdump, vbCrLf, "")
    End If
    
End Function

Private Sub push(ary, Value) 	 
    On Error GoTo init
    x = UBound(ary)  
    ReDim Preserve ary(UBound(ary) + 1)
    ary(UBound(ary)) = Value
    Exit Sub
init: ReDim ary(0): ary(0) = Value
End Sub




note: this function is simple but not safe on systems with extended charsets like chinese. For an internationalized version of these functions please see modEscapes.bas from PDFStreamDumper source. Function unescape(x) On Error GoTo hell Dim tmp() As String Dim b1, b2 Dim i As Long tmp = Split(x, "%") For i = 0 To UBound(tmp) t = tmp(i) If LCase(VBA.Left(t, 1)) = "u" Then If Len(t) >= 5 Then decode = Mid(t, 1, 5) b1 = Mid(decode, 2, 2) b2 = Mid(decode, 4, 2) tmp(i) = cHex(b2) & cHex(b1) If Len(t) > 5 Then tmp(i) = tmp(i) & Mid(t, 6) End If Else If Len(t) >= 2 Then decode = Mid(t, 1, 2) tmp(i) = cHex(decode) If Len(t) > 2 Then tmp(i) = tmp(i) & Mid(t, 3) End If End If Next hell: unescape = Join(tmp, "") If Err.Number <> 0 Then MsgBox "Error in unescape:( " & Err.Description End If End Function Private Function cHex(v) As String On Error Resume Next cHex = Chr(CLng("&h" & v)) If Err.Number <> 0 Then cHex = "?" End Function

C#



using System.IO; using System.Text; public class HexDumper { private const int LineLen = 16; private static int bCount = 0; private static byte[] bytes = new byte[LineLen]; private static StringBuilder buf; public static string HexDump(string str) { buf = new StringBuilder(); char[] ch = str.ToCharArray(); for (int i = 0; i &lt; ch.Length; i++) AddByte((byte)ch[i], (i == ch.Length-1)); return buf.ToString(); } public static string HexDump(byte[] b) { buf = new StringBuilder(); for (int i = 0; i &lt; b.Length; i++) AddByte(b[i], (i==b.Length-1) ); return buf.ToString(); } public static string HexDump(byte[] b, bool showOffset) { buf = new StringBuilder(); for (int i = 0; i < b.Length; i++) { if (showOffset && (i == 0 || i % 16 == 0)) buf.Append(i.ToString("X05")+" "); AddByte(b[i], (i == b.Length - 1)); } return buf.ToString(); } private static void AddByte(byte b, bool final) { bytes[bCount++] = b; if (!final) if(bCount != LineLen) return; if (bCount &lt;= 0) return; //main dump section for (int i = 0; i &lt; LineLen; i++) { buf.Append( i &gt;= bCount ? " " : bytes[i].ToString("X2") + " "); } buf.Append(" "); //char display pad for (int i = 0; i &lt; LineLen; i++) { byte ch = bytes[i] &gt;= 32 && bytes[i] &lt;= 126 ? bytes[i] : (byte)0x2e; //dot buf.Append(i &gt;= bCount ? " " : (char)ch+""); } buf.Append("\n"); bCount = 0; } }


C





void hexdump(unsigned char* str, int len){ char asc[19]; int aspot=0; int i=0; const hexline_length = 3*16+4; char *nl="\n"; char *tmp = (char*)malloc(50); printf("%s",nl); for(i=0;i&lt;len;i++){ sprintf(tmp, "%02x ", str[i]); printf("%s",tmp); if( (int)str[i]&gt;20 && (int)str[i] &lt; 123 ) asc[aspot] = str[i]; else asc[aspot] = 0x2e; aspot++; if(aspot%16==0){ asc[aspot]=0x00; sprintf(tmp," %s\n", asc); printf("%s",tmp); aspot=0; } } if(aspot%16!=0){//print last ascii segment if not full line int spacer = hexline_length - (aspot*3); while(spacer--) printf(" "); asc[aspot]=0x00; sprintf(tmp, "%s\n",asc); printf("%s",tmp); } printf(nl); free(tmp); }

ActionScript



function hexDump(bytes:ByteArray):String { var output:String = ""; var charbuf:String = ""; bytes.position = 0; for (var i:int = 1; i &lt; bytes.length+1; i++) { var byte:int = bytes.readByte(); if( byte&gt;20 && byte &lt; 123 )charbuf+= String.fromCharCode(byte); else charbuf += "."; output += byte2hex(byte) + " "; if(i%16==0){ output += "\t" + charbuf + "\n"; charbuf = ""; } } if(i%16 != 0){ while(i%16!=0){output+=" ";i++;} output+=" " output += "\t" + charbuf + "\n"; } return output; } function byte2hex(byte:uint):String { //http://www.actionscript.org/forums/showthread.php3?t=189952 var hex:String = ""; var arr:String = "FEDCBA"; for(var i:uint = 0; i &lt; 2; i++) { if(((byte & (0xF0 &gt;&gt; (i * 4))) &gt;&gt; (4 - (i * 4))) &gt; 9){ hex += arr.charAt(15 - ((byte & (0xF0 &gt;&gt; (i * 4))) &gt;&gt; (4 - (i * 4)))); } else{ hex += String((byte & (0xF0 &gt;&gt; (i * 4))) &gt;&gt; (4 - (i * 4))); } } return hex; }

PHP Hexdump function (not mine)



function hdump($data, $htmloutput = true, $uppercase = false, $return = false){ //Init $hexi = ""; $ascii = ""; $dump = ($htmloutput === true) ? "&lt;pre&gt;" : ""; $offset = 0; $len = strlen($data); // Upper or lower case hexidecimal $x = ($uppercase === false) ? "x" : "X"; // Iterate string for ($i = $j = 0; $i &lt; $len; $i++) { // Convert to hexidecimal $hexi .= sprintf("%02$x ", ord($data[$i])); // Replace non-viewable bytes with "." if (ord($data[$i]) &gt;= 32) { $ascii .= ($htmloutput === true) ? htmlentities($data[$i]) : $data[$i]; } else { $ascii .= "."; } // Add extra column spacing if ($j === 7) { $hexi .= " "; $ascii .= " "; } // Add row if (++$j === 16 || $i === $len - 1) { // Join the hexi / ascii output $dump .= sprintf("%04$x %-49s %s", $offset, $hexi, $ascii); // Reset vars $hexi = $ascii = ""; $offset += 16; $j = 0; // Add newline if ($i !== $len - 1) { $dump .= "\n"; } } } // Finish dump $dump .= $htmloutput === true ? "&lt;/pre&gt;" : ""; $dump .= "\n"; // Output method if ($return === false) { echo $dump; } else { return $dump; } }




RSS Feed
About Me
Home

Posts:
C# App Config
Tero DES C# Test
VC 2008 Bit Fields
Speed trap
C# Db Class Generator
VB6 vrs .NET (again)
FireFox Whois Extension
git and vb6
Code Additions
Compiled date to string
C# ListView Sorter
VB6 Wish List
C# Process Injection
CAPTCHA Bots
C# PE Offset Calculator
VB6 Async Download
Show Desktop
Code release
Dll Not Found in IDE
Advanced MSScript Control
random tip
Clipart / Vector Art
VB6 Callback from C#
Binary data from VB6 to C#
CSharp and MsScriptControl
HexDumper functions
Js Beautify From VB
vb6 FormPos
Inline Asm w VB6
The .NET Fiasco
One rub on computers
Universal extractor


Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 34 + 16 = ? followed by the letter: H