* * * * *

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

Jeff Cuscutis [1] 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 [2], 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
> // 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: [email protected]
> //
> // ***********************************************************************
>
> // 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);
>             }
>             else
>             {
>                 foreach (var fileName in args)
>                 {
>                     try
>                     {
>                         using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
>                         {
>                             TextReader tr = new StreamReader(file, Encoding.ASCII);
>                             Console.Out.WriteLine("-----{0}-----",fileName);
>                             DoDump(tr, Console.Out);
>                             file.Close();
>                         }
>                     }
>                     catch (Exception e)
>                     {
>                         Console.Error.WriteLine(e.Message);
>                     }
>
>                 }
>             }
>         }
>
>         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;
>                 do
>                 {
>                     outFile.Write("{0:X2} ", (byte)buf[j]);
>                     if (!char.IsControl(buf[j]))
>                         display[j] = 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();
>             }
>         }
>     }
> }
>

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.

* Part 24: more lookup tables [3]
* Part 26: C89, system calls and mmap() [4]

[1] http://spinthecat.blogspot.com/
[2] http://en.wikipedia.org/wiki/Mono_(software)
[3] gopher://gopher.conman.org/0Phlog:2012/02/01.3
[4] gopher://gopher.conman.org/0Phlog:2012/02/03.1

Email author at [email protected]