The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Saturday, February 04, 2012

99 ways to program a hex, Part 27: C♯, binary stream

Jeff Cuscutis sent in another version of the C♯ program. He writes:

From
Jeffrey Cuscutis <XXXXXXXXXXXXXXXXXXXXXXX>
To
Sean Conner <sean@conman.org>
Subject
Re: 99 Programs
Date
Sat, 4 Feb 2012 22:51:37 -0500

Modified to use BinaryReader instead of TextReader. It sort of worked on binary files before this, but replaced unprintable characters with “?” automatically when read.

It now does this correctly, but I had to make a wrapper function to read data from the Console as that is a TextReader.

// *************************************************************************
//
// Copyright 2012 by Jeff Cuscutis.  All Rights Reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Comments, questions and criticisms can be sent to: sean@conman.org
//
// ***********************************************************************

// C#, binary stream

using System;
using System.IO;

namespace Hex
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                DoDump(ReadFromConsole, Console.Out);
            }
            else
            {
                foreach (var fileName in args)
                {
                    try
                    {
                        using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                        {
                            var tr = new BinaryReader(file);
                            Console.Out.WriteLine("-----{0}-----",fileName);
                            DoDump(tr.Read, Console.Out);
                            file.Close();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e.Message);
                    }
                    
                }
            }
        }

        // wrapper to fake reading from a TextReader to 
        // make it look like it is a BinaryReader
        static int ReadFromConsole(byte[] buf, int index, int count)
        {
            var charBuf = new char[count];

            int actuallyRead = Console.In.Read(charBuf, index, count);

            for (int i = 0; i < charBuf.Length; i++)
            {
                buf[i] = (byte)charBuf[i];
            }

            return actuallyRead;
        }

        static void DoDump(Func<byte[], int, int, int> readFunc, TextWriter outFile)
        {
            const int blockLength = 16;
            int actuallyRead;
            var buf = new byte[blockLength];
            var offset = 0;

            while ((actuallyRead = readFunc(buf, 0, blockLength)) > 0)
            {
                var display = new char[blockLength+1];

                outFile.Write("{0:X8} ",offset);

                var j = 0;
                do
                {
                    outFile.Write("{0:X2} ", buf[j]);
                    if (!char.IsControl((char)buf[j]))
                        display[j] = (char)buf[j];
                    else
                        display[j] = '.';
                    offset++;
                    j++;
                    actuallyRead--;
                } while ((j < blockLength) && (actuallyRead > 0));
                display[blockLength] = '\0';

                if (j < blockLength)
                    for (var i = j; i < blockLength; i++) outFile.Write("   ");

                outFile.WriteLine(display);

                outFile.Flush();
            }
        }
    }
}

Update on Monday, February 6th, 2012

Jeff wrote me to add:

I forgot to mention that it also uses a generic signature to handle the readFunc parameter in DoDump()

Obligatory Picture

[The future's so bright, I gotta wear shades]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: https://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

https://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.