The hex dump program. : StringWriter « File Stream « C# / C Sharp






The hex dump program.

  


using System;
using System.IO;


class Class1 {
    public static string Pad(string s, int len) {
        string temp = s;
        for (int i = s.Length; i < len; ++i)
            temp = "0" + temp;
        return temp;
    }
    static void Main(string[] args) {
        StreamReader sr = new StreamReader("c:\\a.txt");
        string line = "";
        int nCounter = 0;
        int nOffset = 0;
        while ((line = sr.ReadLine()) != null) {
            for (int i = 0; i < line.Length; ++i) {
                int c = (int)line[i];
                string fmt = String.Format("{0:x}", c);
                if (fmt.Length == 1)
                    fmt = Pad(fmt, 2);
                if (nOffset % 16 == 0) {
                    string offsetFmt = nOffset.ToString();

                    System.Console.Write(Pad(offsetFmt, 5) + ": ");
                }

                System.Console.Write(fmt + " ");
                if (nCounter == 15) {
                    System.Console.Write("\n");
                    nCounter = 0;
                } else
                    nCounter++;
                nOffset++;
            }
        }
    }
}

   
  








Related examples in the same category

1.Use StringWriter to write string
2.StringWriter implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.
3.Initializes a new instance of the StringWriter class with the specified format control.
4.Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.
5.Initializes a new instance of the StringWriter class.