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.java
bin2ascii_test.java


bin2ascii.java

Synopsis
package jsnippets;

import java.io.*;

class bin2ascii
    {
    static public void write(OutputStream os, byte[] buffer, int len)
      {
      new bin2ascii(os, buffer, len);
      }

    private int cBytesPerSegment = 16;
    private int cBytesPerHalfSegment = 8;
    private OutputStreamWriter mOut;
    private byte[] mBuffer;
    private int mSegmentStart;
    private int mSegmentLength;
    private int mCurrentPosition;

    private bin2ascii(OutputStream os, byte[] buffer, int len)
      {
      mOut = new OutputStreamWriter(os);
      mBuffer = buffer;
      if (len == 0) return ;
      if (mBuffer == null) return ;
      ConvertBufferSegmentBySegment(len);
      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;
    }
    private interface WriteFunc
    {
        void writeByte(byte b);
    }
    private class WriteByteAsHex implements WriteFunc
      {
        public void writeByte(byte b)
          {
          final char[] hexdigits = "0123456789ABCDEF".toCharArray();

          int i = b >= 0 ? b : 256 + b;
          write("" + hexdigits[i / 16] + hexdigits[i % 16] + " ");
          }
      }

   private class WriteByteAsAscii implements WriteFunc
      {
        public void writeByte(byte b)
        {
        char c = (b > 0x1F && b < 0x7F) ? (char)b : '.';
        write("" + c);
        }
      }

    private void WriteSegment()
      {
      WriteSegmentUsing(new WriteByteAsHex());
      WriteRemainingPadding();
      WriteIntraColumnGap();
      WriteSegmentUsing(new WriteByteAsAscii());
      write("\n");
      }
    private void WriteRemainingPadding()
      {
      for ( ; mCurrentPosition < cBytesPerSegment; ++mCurrentPosition)
          {
          WriteHalfSegmentGap();
          write("   ");
          }
      }
    private void WriteIntraColumnGap()
      {
      write("  ");
      }
    private void WriteSegmentUsing(WriteFunc wf)
      {
      for (mCurrentPosition = 0; mCurrentPosition < mSegmentLength; ++mCurrentPosition)
          {
          WriteHalfSegmentGap();
          wf.writeByte(mBuffer[mSegmentStart + mCurrentPosition]);
          }
      }
    private void WriteHalfSegmentGap()
      {
      if (mCurrentPosition == cBytesPerHalfSegment)
        write("  ");
      }

    private void write(String s)
      {
      try
          {
          mOut.write(s);
          }
      catch (Exception ex)
        {}
      }

    private void flush()
      {
      try
          {
          mOut.flush();
          }
      catch (Exception ex)
        {}
      }
    }


bin2ascii_test.java

Synopsis
package jsnippets;
import java.io.*;
import utjava.ut;

public class bin2ascii_test
    {
    public void testemptybuffer1()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      bin2ascii.write(s, null, 0);
      ut.utassert(s.toString(), "");
      }

    public void testemptybuffer2()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      byte[] buf = StringToByteArray("abc");
      bin2ascii.write(s, buf, 0);
      ut.utassert(s.toString(), "");
      }

    public void test1char()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      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
      ut.utassert(s.toString(), "30                                                  0\n");
      }

    public void test8chars()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      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
      ut.utassert(s.toString(), "30 31 32 33 34 35 36 37                             01234567\n");
      }

    public void test9chars()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      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
      ut.utassert(s.toString(), "30 31 32 33 34 35 36 37   38                        01234567  8\n");
      }

    public void test15chars()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      //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
      ut.utassert(s.toString(), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F      abcdefgh  ijklmno\n");
      }

    public void test16chars()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      //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
      ut.utassert(s.toString(), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F 70   abcdefgh  ijklmnop\n");
      }

    public void test17chars()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      //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
      ut.utassert(s.toString(), "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()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      //    byte[] buf = StringToByteArray("\x7F\x80\xFE\xFF\x01\n\t\r");
      byte[] buf = { 0x7F, -128, -2, -1, 0x01, 0x0A, 0x09, 0x0D};
      bin2ascii.write(s, buf, buf.length);
      ut.utassert(s.toString(), "7F 80 FE FF 01 0A 09 0D                             ........\n");
      }

    public void testallcharpatterns()
      {
      ByteArrayOutputStream s = new ByteArrayOutputStream();
      byte[] buf = new byte[256 + 5];
      for (int 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
      ut.utassert(s.toString(),
                  "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] = (byte) schars[i];
      return sbytes;
      }

    //  string StreamToString(ByteArrayOutputStream 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