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.h
bin2ascii_test.cpp


bin2ascii.h

Synopsis
#include <ostream>
#include <iomanip>
using namespace std;

class bin2ascii
  {
  public:
    static void write(ostream& os, char* buffer, int len)
      {
      bin2ascii b2a(os, buffer, len);
      }

  private:
    bin2ascii(ostream& os, char* buffer, int len) 
      : cCharsPerSegment(16), cCharsPerHalfSegment(8), mOut(os),
      mBuffer(buffer) 
      {
      if (len == 0) return;
      if (mBuffer == 0) return;
      ConvertBufferSegmentBySegment(len);
      }
    void ConvertBufferSegmentBySegment(int len)
      {
      for(mSegmentStart = 0;mSegmentStart < len; mSegmentStart += cCharsPerSegment)
        {
        CalculateCurrentSegmentLength(len);
        WriteSegment();
        }
      }
    void CalculateCurrentSegmentLength(int len)
      {
      mSegmentLength = len - mSegmentStart > cCharsPerSegment ? cCharsPerSegment : len - mSegmentStart;
      }
    typedef void (bin2ascii::* WriteFunc) (char c);
    void WriteSegment()
      {
      WriteSegmentUsing(&bin2ascii::WriteCharAsHex);
      WriteRemainingPadding();
      WriteIntraColumnGap();
      WriteSegmentUsing(&bin2ascii::WriteCharAsAscii);
      mOut << "\n";
      }
    void WriteRemainingPadding()
      {
      for( ; mCurrentPosition < cCharsPerSegment; ++mCurrentPosition)
        {
        WriteHalfSegmentGap();
        mOut << "   ";
        }
      }
    void WriteIntraColumnGap()
      {
      mOut << "  ";
      }
    void WriteSegmentUsing(WriteFunc wf)
      {
      for (mCurrentPosition = 0; mCurrentPosition < mSegmentLength; ++mCurrentPosition)
        {
        WriteHalfSegmentGap();
        (this->*wf)(mBuffer[mSegmentStart + mCurrentPosition]);
        }
      }
    void  WriteHalfSegmentGap()
      {
      if (mCurrentPosition == cCharsPerHalfSegment)
        mOut << "  ";
      }

    void WriteCharAsAscii(char c)
      {
      mOut << (isprint(c & 0x0FF) ? c : '.');
      }
    void WriteCharAsHex(char c)
      {
      ios::fmtflags oldflags = mOut.flags();
      mOut.setf(ios::hex,   ios::basefield);
      mOut.setf(ios::right, ios::adjustfield);
      mOut << setw(2) << setfill('0') << uppercase 
           << (c & 0x0FF) << ' ';
      mOut.setf(oldflags);
      }

  private:
    const int cCharsPerSegment;
    const int cCharsPerHalfSegment;
    ostream& mOut;
    const char* const mBuffer;
    int mSegmentStart;
    int mSegmentLength;
    int mCurrentPosition;
  } ;


bin2ascii_test.cpp

Synopsis
#include <strstream>

#include "utx.h"

#include "bin2ascii.h"

TEST(emptybuffer1)
  {
  strstream s;
  bin2ascii::write(s, 0, 0);
  s << ends;
  utxassert(s.str(), "");
  }

TEST(emptybuffer2)
  {
  strstream s;
  char* buf = "abc";
  bin2ascii::write(s, buf, 0);
  s << ends;
  utxassert(s.str(), "");
  }

TEST(1char)
  {
  strstream s;
  char* buf = "0";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 . 32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "30                                                  0\n");
  }

TEST(8chars)
  {
  strstream s;
  char* buf = "01234567";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "30 31 32 33 34 35 36 37                             01234567\n");
  }

TEST(9chars)
  {
  strstream s;
  char* buf = "012345678";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "30 31 32 33 34 35 36 37   38                        01234567  8\n");
  }

TEST(15chars)
  {
  strstream s;
  //12345678901234567 
  char* buf = "abcdefghijklmno";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F      abcdefgh  ijklmno\n");
  }

TEST(16chars)
  {
  strstream s;
  //12345678901234567 
  char* buf = "abcdefghijklmnop";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F 70   abcdefgh  ijklmnop\n");
  }

TEST(17chars)
  {
  strstream s;
  //12345678901234567 
  char* buf = "abcdefghijklmnopq";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "61 62 63 64 65 66 67 68   69 6A 6B 6C 6D 6E 6F 70   abcdefgh  ijklmnop\n"
                    "71                                                  q\n");
  }

TEST(highvalchars)
  {
  strstream s;
  //12345678901234567 
  char* buf = "\x7F\x80\xFE\xFF\x01\n\t\r";
  bin2ascii::write(s, buf, strlen(buf));
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "7F 80 FE FF 01 0A 09 0D                             ........\n");
  }

TEST(streamflags)
  {
  strstream s;
  //12345678901234567 
  char* buf = "\x7f";
  bin2ascii::write(s, buf, strlen(buf));
  s << 8;
  s << hex << '\x0F';
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), "7F                                                  .\n8\x0F");
  }

TEST(allcharpatterns)
  {
  strstream s;
  char* buf = new char[256 + 5];
  for(unsigned i = 0; i < 256; ++i)
    {
    buf[i] = i & 0x0FF;
    }
  bin2ascii::write(s, buf, 256);
  delete [] buf;
  s << ends;       //32 32 32 32 32 32 32 32 ..32 32 32 32 32 32 32 32   0
  utxassert(s.str(), 
    "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"
    );
  }






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-2010