# 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 &gt; 32 And x &lt; 127), Chr(x), ".")
If i &gt; 1 And i Mod 16 = 0 Then
h = Hex(offset)
While Len(h) &lt; 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 &lt;&gt; Empty Then
If hexOnly = 0 Then
h = Hex(offset)
While Len(h) &lt; 6: h = "0" & h: Wend
h = h & "   " & tmp
While Len(h) &lt;= 56: h = h & " ": Wend
push s, h & chars
Else
push s, tmp
End If
End If

Hexdump = Join(s, vbCrLf)

If hexOnly &lt;&gt; 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) &gt;= 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) &gt; 5 Then tmp(i) = tmp(i) & Mid(t, 6)
End If
Else
If Len(t) &gt;= 2 Then
decode = Mid(t, 1, 2)
tmp(i) = cHex(decode)
If Len(t) &gt; 2 Then tmp(i) = tmp(i) & Mid(t, 3)
End If
End If

Next

hell:
unescape = Join(tmp, "")

If Err.Number &lt;&gt; 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 &lt;&gt; 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("  ");

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++)
{
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 {
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 .= ".";
}

if (\$j === 7) {
\$hexi  .= " ";
\$ascii .= " ";
}

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;

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;
}
}

```