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.

Thursday, February 02, 2012

99 ways to program a hex, Part 25: C♯

Jeff Cuscutis sent in a version written in C♯ (C-sharp, in case you don't have a font with the sharp symbol). He assured me the code works, but I can't test it as I don't use Microsoft Windows; nor have I installed Mono, as I don't really have a need to interoperate with the Microsoft Windows environment (at home, or at The Corporation).

// *************************************************************************
// 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
// 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:
// ***********************************************************************

// C#

using System;
using System.IO;
using System.Text;

namespace Hex
    class Program
        static void Main(string[] args)
            if (args.Length == 0)
                DoDump(Console.In, Console.Out);
                foreach (var fileName in args)
                        using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                            TextReader tr = new StreamReader(file, Encoding.ASCII);
                            DoDump(tr, Console.Out);
                    catch (Exception e)

        static void DoDump(TextReader inFile, TextWriter outFile)
            const int blockLength = 16;
            int actuallyRead;
            var buf = new char[blockLength];
            var offset = 0;

            while ((actuallyRead = inFile.Read(buf, 0, blockLength)) > 0)
                var display = new char[blockLength+1];

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

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

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



About the only question I have about this version is that it appears to open the input file in text mode, and the hex dump program should work on any type of file, text or binary.

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:, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

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.