I always have to search for these.

VB6 Hexdump function

Function HexDump(strOrAry, 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, i As Long, tt, x, h, j
    Const LANG_US = &H409
    
    offset = 0

    If TypeName(strOrAry) = "Byte()" Then
        ary = strOrAry
    Else
        ary = StrConv(strOrAry, vbFromUnicode, LANG_US)
    End If
    
    j = 1
    chars = "   "
    For i = 0 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 < &H7D), Chr(x), ".")
        If j 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
        j = j + 1
    Next
    
    'if read length was not mod 16=0 then
    'we have part of line to account for
    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

Sub push(ary, value) 'this modifies parent ary object
    On Error GoTo init
    Dim x As Long
    x = UBound(ary) '<-throws Error If Not initalized
    ReDim Preserve ary(UBound(ary) + 1)
    ary(UBound(ary)) = value
    Exit Sub
init:     ReDim ary(0): ary(0) = value
End Sub

C

void real_hexdump(unsigned char* str, int len, int offset, bool hexonly){ char asc[19]; int aspot=0; int i=0; int hexline_length = 3*16+4; char *nl="\n"; char *tmp = (char*)malloc(75); bool color_on = false; uint32_t display_rows = -1; uint32_t displayed_lines = -1; CONSOLE_SCREEN_BUFFER_INFO csb; if(GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE) , &csb) !=0){ display_rows = csb.srWindow.Bottom - csb.srWindow.Top - 2; } if(!hexonly) printf(nl); if(offset >=0){ printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n"); printf("%04x ", offset); } for(i=0;i20 && (int)str[i] < 123 ) asc[aspot] = str[i]; else asc[aspot] = 0x2e; aspot++; if(aspot%8==0) printf(" "); //to make figuring out offset easier if(aspot%16==0){ asc[aspot]=0x00; if(!hexonly){ displayed_lines++; sprintf(tmp," %s\n", asc); printf("%s",tmp); if(display_rows > 0 && displayed_lines == display_rows){ if(!opts.automationRun){ displayed_lines = 0; printf("-- More --"); char qq = getch(); if(qq == 'q') break; printf("\n"); } } } if(offset >=0){ offset += 16; if(i+1 != len) printf("%04x ", offset); } aspot=0; } } if(aspot%16!=0){//print last ascii segment if not full line if(!hexonly){ int spacer = hexline_length - (aspot*3); while(spacer--) printf("%s"," "); asc[aspot]=0x00; sprintf(tmp, "%s\n",asc); printf("%s",tmp); } } if(!hexonly) printf("%s",nl); free(tmp); }

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 < 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 < 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 <= 0) return; //main dump section for (int i = 0; i < LineLen; i++) { buf.Append( i >= bCount ? " " : bytes[i].ToString("X2") + " "); } buf.Append(" "); //char display pad for (int i = 0; i < LineLen; i++) { byte ch = bytes[i] >= 32 && bytes[i] <= 126 ? bytes[i] : (byte)0x2e; //dot buf.Append(i >= bCount ? " " : (char)ch+""); } buf.Append("\n"); bCount = 0; } }

Pascal

function HexDump(data:string;maxLen:integer = -1):string; var i: integer; buf: string; tmp: string; chars: string; b: byte; charsOffset: integer; begin charsOffset := 0; tmp := format('%.8x', [0]) + ' '; if (maxLen = -1) or (maxLen > length(data)) then maxLen := length(data); for i:=1 to maxLen do begin b := ord(data[i]); tmp += format('%.2x', [b] ) + ' '; if (b > 32) and (b < 127) then chars += data[i] else chars += '.'; if i mod 16 = 0 then begin tmp += ' '; if charsOffset = 0 then charsOffset := length(tmp); tmp += chars + #13#10; buf += tmp; tmp := format('%.8x', [i]) + ' '; //starting a new line.. chars := ''; end; end; if length(chars) > 0 then begin charsOffset -= length(tmp); buf += tmp + StringOfChar(' ',charsOffset) + chars; end; Result := buf; end;

ActionScript

public function hexdump(bytes:ByteArray, start:uint = 1, length:uint = 0 ):void { var output:String = ""; var charbuf:String = ""; if(start==0) start = 1; if(length > bytes.length || length == 0) length = bytes.length; bytes.position = start - 1; //0 based for (var i:int = start; i < length+1; i++) { var byte:int = bytes.readByte(); if( byte>20 && byte < 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"; } xx(output); //return output; } public 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 < 2; i++) { if(((byte & (0xF0 >> (i * 4))) >> (4 - (i * 4))) > 9){ hex += arr.charAt(15 - ((byte & (0xF0 >> (i * 4))) >> (4 - (i * 4)))); } else{ hex += String((byte & (0xF0 >> (i * 4))) >> (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) ? "<pre>" : ""; $offset = 0; $len = strlen($data); // Upper or lower case hexidecimal $x = ($uppercase === false) ? "x" : "X"; // Iterate string for ($i = $j = 0; $i < $len; $i++) { // Convert to hexidecimal $hexi .= sprintf("%02$x ", ord($data[$i])); // Replace non-viewable bytes with "." if (ord($data[$i]) >= 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 ? "</pre>" : ""; $dump .= "\n"; // Output method if ($return === false) { echo $dump; } else { return $dump; } }

Python Hexdump function (sbz gist)

def hexdump(src, length=16): FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)]) lines = [] for c in xrange(0, len(src), length): chars = src[c:c+length] hex = ' '.join(["%02x" % ord(x) for x in chars]) printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars]) lines.append("%04x %-*s %s\n" % (c, length*3, hex, printable)) return ''.join(lines)