* * * * *

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

Jeff Cuscutis [1] sent in another version of the C♯ program [2]. He writes:

> From: Jeffrey Cuscutis <XXXXXXXXXXXXXXXXXXXXXXX>
> To: Sean Conner <[email protected]>
> 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: [email protected]
> //
> // ***********************************************************************
>
> // 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();
>             }
>         }
>     }
> }
>

* Part 26: C89, system calls and mmap() [3]
* Part 28: K&R, system calls, full buffering [4]

Update on Monday, February 6^th, 2012

Jeff wrote me to add:

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


[1] http://spinthecat.blogspot.com/
[2] gopher://gopher.conman.org/0Phlog:2012/02/02.1
[3] gopher://gopher.conman.org/0Phlog:2012/02/03.1
[4] gopher://gopher.conman.org/0Phlog:2012/02/05.1

Email author at [email protected]