binary to ascii dump

binary to ascii dump (n bytes wide with the ascii equivalents)

given a memory buffer with 8-bit bytes, this routine will print out the ascii equivalent in hex format and if the charachert is printable, the glyph.

The number of columns per line is adjustable.

Example:

assume 0x0000F000 contains the values: ABF0303132

0000F000: AB F0 30 31 32 ..012

Download hextoasciidump.zip

Synopsis:

bin2ascii.cs
bin2ascii_test.cs


bin2ascii.cs

Synopsis
using System;
using System.IO;

class bin2ascii
{
  static public void write(Stream os, byte[] buffer, int len)
  {
    new bin2ascii(os, buffer, len);
  }
  
  private const int cBytesPerSegment = 16;
  private const int cBytesPerHalfSegment = 8;
  private StreamWriter mOut;
  private readonly byte[] mBuffer;
  private int mSegmentStart;
  private int mSegmentLength;
  private int mCurrentPosition;
  
  private bin2ascii(Stream os, byte[] buffer, int len) 
  {
    mOut = new StreamWriter(os);
    mBuffer = buffer;
    if (len == 0) return;
    if (mBuffer == null) return;
    ConvertBufferSegmentBySegment(len);
    mOut.Flush();
  }
  private void ConvertBufferSegmentBySegment(int len)
  {
    for(mSegmentStart = 0;mSegmentStart < len; mSegmentStart += cBytesPerSegment)
    {
      CalculateCurrentSegmentLength(len);
      WriteSegment();
    }
  }
  private void CalculateCurrentSegmentLength(int len)
  {
    mSegmentLength = len - mSegmentStart > cBytesPerSegment ? cBytesPerSegment : len - mSegmentStart;
  }
  delegate  void WriteFunc(byte c);
  private void WriteSegment()
  {
    WriteSegmentUsing(new WriteFunc(WriteByteAsHex));
    WriteRemainingPadding();
    WriteIntraColumnGap();
    WriteSegmentUsing(new WriteFunc(WriteByteAsAscii));
    mOut.Write('\n');
  }
  private void WriteRemainingPadding()
  {
    for( ; mCurrentPosition < cBytesPerSegment; ++mCurrentPosition)
    {
      WriteHalfSegmentGap();
      mOut.Write("   ");
    }
  }
  private void WriteIntraColumnGap()
  {
    mOut.Write("  ");
  }
  private void WriteSegmentUsing(WriteFunc wf)
  {
    for (mCurrentPosition = 0; mCurrentPosition < mSegmentLength; ++mCurrentPosition)
    {
      WriteHalfSegmentGap();
      wf(mBuffer[mSegmentStart + mCurrentPosition]);
    }
  }
  private void  WriteHalfSegmentGap()
  {
    if (mCurrentPosition == cBytesPerHalfSegment)
      mOut.Write("  ");
  }
  
  private void WriteByteAsAscii(byte b)
  {
    char c = (b > 0x1F && b < 0x7F) ? Convert.ToChar(b) : '.';
    mOut.Write(c);
  }
  private void WriteByteAsHex(byte b)
  {
    mOut.Write("{0, 2:X02} ", b);
  }
} ;


bin2ascii_test.cs

Synopsis
using System;
using System.IO;
using ut;

public class testbin2ascii
{
  public void testemptybuffer1()
  {
    MemoryStream s = new MemoryStream();
    bin2ascii.write(s, null, 0);
    utx.assert(StreamToString(s), "");
  }

  public void testemptybuffer2()
  {
    MemoryStream s = new MemoryStream();
    byte[] buf = StringToByteArray("abc");
    bin2ascii.write(s, buf, 0);
    utx.assert(StreamToString(s), "");
  }

  public void test1char()
  {
    MemoryStream s = new MemoryStream();
    byte[] buf = StringToByteArray("0");
    bin2ascii.write(s, buf, buf.Length);
    //32 32 32 32 32 32 32 32 . 32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), "30                                                  0\n");
  }

  public void test8chars()
  {
    MemoryStream s = new MemoryStream();
    byte[] buf = StringToByteArray("01234567");
    bin2ascii.write(s, buf, buf.Length);
    //            32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), "30 31 32 33 34 35 36 37                             01234567\n");
  }
  
  public void test9chars()
    {
    MemoryStream s = new MemoryStream();
    byte[] buf = StringToByteArray("012345678");
    bin2ascii.write(s, buf, buf.Length);
           //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), "30 31 32 33 34 35 36 37   38                        01234567  8\n");
    }
  
  public void test15chars()
    {
    MemoryStream s = new MemoryStream();
    //12345678901234567 
    byte[] buf = StringToByteArray("abcdefghijklmno");
    bin2ascii.write(s, buf, buf.Length);
           //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F      abcdefgh  ijklmno\n");
    }
  
  public void test16chars()
    {
    MemoryStream s = new MemoryStream();
    //12345678901234567 
    byte[] buf = StringToByteArray("abcdefghijklmnop");
    bin2ascii.write(s, buf, buf.Length);
           //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F 70   abcdefgh  ijklmnop\n");
    }
  
  public void test17chars()
    {
    MemoryStream s = new MemoryStream();
    //12345678901234567 
    byte[] buf = StringToByteArray("abcdefghijklmnopq");
    bin2ascii.write(s, buf, buf.Length);
           //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F 70   abcdefgh  ijklmnop\n" +
                      "71                                                  q\n");
    }
  
  public void testhighvalchars()
    {
    MemoryStream s = new MemoryStream();
    byte[] buf = StringToByteArray("\x7F\x80\xFE\xFF\x01\n\t\r");
    bin2ascii.write(s, buf, buf.Length);
    utx.assert(StreamToString(s), "7F 80 FE FF 01 0A 09 0D                             ........\n");
    }
  
  public void testallcharpatterns()
    {
    MemoryStream s = new MemoryStream();
    byte[] buf = new byte[256 + 5];
    for(uint i = 0; i < 256; ++i)
      {
      buf[i] = (byte)i;
      }
    bin2ascii.write(s, buf, 256);
           //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
    utx.assert(StreamToString(s), 
      "00 01 02 03 04 05 06 07   08 09 0A 0B 0C 0D 0E 0F   ........  ........\n" +
      "10 11 12 13 14 15 16 17   18 19 1A 1B 1C 1D 1E 1F   ........  ........\n" +
      "20 21 22 23 24 25 26 27   28 29 2A 2B 2C 2D 2E 2F    !\"#$%&'  ()*+,-./\n" +
      "30 31 32 33 34 35 36 37   38 39 3A 3B 3C 3D 3E 3F   01234567  89:;<=>?\n" +
      "40 41 42 43 44 45 46 47   48 49 4A 4B 4C 4D 4E 4F   @ABCDEFG  HIJKLMNO\n" +
      "50 51 52 53 54 55 56 57   58 59 5A 5B 5C 5D 5E 5F   PQRSTUVW  XYZ[\\]^_\n" +
      "60 61 62 63 64 65 66 67   68 69 6A 6B 6C 6D 6E 6F   `abcdefg  hijklmno\n" +
      "70 71 72 73 74 75 76 77   78 79 7A 7B 7C 7D 7E 7F   pqrstuvw  xyz{|}~.\n" +
      "80 81 82 83 84 85 86 87   88 89 8A 8B 8C 8D 8E 8F   ........  ........\n" +
      "90 91 92 93 94 95 96 97   98 99 9A 9B 9C 9D 9E 9F   ........  ........\n" +
      "A0 A1 A2 A3 A4 A5 A6 A7   A8 A9 AA AB AC AD AE AF   ........  ........\n" +
      "B0 B1 B2 B3 B4 B5 B6 B7   B8 B9 BA BB BC BD BE BF   ........  ........\n" +
      "C0 C1 C2 C3 C4 C5 C6 C7   C8 C9 CA CB CC CD CE CF   ........  ........\n" +
      "D0 D1 D2 D3 D4 D5 D6 D7   D8 D9 DA DB DC DD DE DF   ........  ........\n" +
      "E0 E1 E2 E3 E4 E5 E6 E7   E8 E9 EA EB EC ED EE EF   ........  ........\n" +
      "F0 F1 F2 F3 F4 F5 F6 F7   F8 F9 FA FB FC FD FE FF   ........  ........\n" 
      );
    }
  byte[] StringToByteArray(string s)
  {
    char[] schars = s.ToCharArray();
    byte[] sbytes = new byte[s.Length];
    for(int i = 0; i < s.Length; ++i)
      sbytes[i] = Convert.ToByte(schars[i]);
    return sbytes;
  }

  string StreamToString(MemoryStream s)
  {
    string str = "";
    if (s.Length == 0) return str;
    byte[] sbytes = s.GetBuffer();
    foreach(byte b in sbytes)
    {
      if (b == 0) break;
      str += Convert.ToChar(b);
    }
    return str;
  }
}






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001,2002,2003,2004,2005,2006,2007