public static void HexDump(Stream input, Stream output, int bytesPerLine = 16) { long pos = input.Position; HexInputFunc inputFunc = delegate(byte[] buf, int max) { return(input.Read(buf, 0, max)); }; HexOutputFunc outputFunc = delegate(char[] line) { byte[] buf = System.Text.Encoding.ASCII.GetBytes(line); output.Write(buf, 0, buf.Length); }; HexDump(inputFunc, outputFunc, bytesPerLine); input.Seek(pos, SeekOrigin.Begin); }
public static void HexDump(byte[] buffer, int offset, int count, Stream output, int bytesPerLine = 16) { HexInputFunc inputFunc = delegate(byte[] buf, int max) { int len = Math.Min(count, max); Buffer.BlockCopy(buffer, offset, buf, 0, len); offset += len; count -= len; return(len); }; HexOutputFunc outputFunc = delegate(char[] line) { byte[] buf = System.Text.Encoding.ASCII.GetBytes(line); output.Write(buf, 0, buf.Length); }; HexDump(inputFunc, outputFunc, bytesPerLine); }
public static string HexDump(Stream input, int bytesPerLine = 16) { StringBuilder sb = new StringBuilder(); long pos = input.Position; HexInputFunc inputFunc = delegate(byte[] buf, int max) { return(input.Read(buf, 0, max)); }; HexOutputFunc outputFunc = delegate(char[] line) { sb.Append(line); }; HexDump(inputFunc, outputFunc, bytesPerLine); input.Seek(pos, SeekOrigin.Begin); return(sb.ToString()); }
public static string HexDump(byte[] buffer, int offset, int count, int bytesPerLine = 16) { StringBuilder sb = new StringBuilder(); HexInputFunc inputFunc = delegate(byte[] buf, int max) { int len = Math.Min(count, max); Buffer.BlockCopy(buffer, offset, buf, 0, len); offset += len; count -= len; return(len); }; HexOutputFunc outputFunc = delegate(char[] line) { sb.Append(line); }; HexDump(inputFunc, outputFunc, bytesPerLine); return(sb.ToString()); }
private static void HexDump(HexInputFunc input, HexOutputFunc output, int bytesPerLine = 16) { byte[] bytes = new byte[bytesPerLine]; char[] HexChars = "0123456789ABCDEF".ToCharArray(); int firstHexColumn = 8 // 8 characters for the address + 3; // 3 spaces int firstCharColumn = firstHexColumn + bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space + (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th + 2; // 2 spaces int lineLength = firstCharColumn + bytesPerLine // - characters to show the ascii value + Environment.NewLine.Length; // Carriage return and line feed (should normally be 2) char[] line = (new System.String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray(); for (int i = 0; ; i += bytesPerLine) { int readLen = input(bytes, bytesPerLine); if (readLen == 0) { break; } line[0] = HexChars[(i >> 28) & 0xF]; line[1] = HexChars[(i >> 24) & 0xF]; line[2] = HexChars[(i >> 20) & 0xF]; line[3] = HexChars[(i >> 16) & 0xF]; line[4] = HexChars[(i >> 12) & 0xF]; line[5] = HexChars[(i >> 8) & 0xF]; line[6] = HexChars[(i >> 4) & 0xF]; line[7] = HexChars[(i >> 0) & 0xF]; int hexColumn = firstHexColumn; int charColumn = firstCharColumn; for (int j = 0; j < bytesPerLine; j++) { if (j > 0 && (j & 7) == 0) { hexColumn++; } if (j >= readLen) { line[hexColumn] = ' '; line[hexColumn + 1] = ' '; line[charColumn] = ' '; } else { byte b = bytes[j]; line[hexColumn] = HexChars[(b >> 4) & 0xF]; line[hexColumn + 1] = HexChars[b & 0xF]; line[charColumn] = (b < 32 ? '·' : (char)b); } hexColumn += 3; charColumn++; } output(line); } }