Advertisement
Guest User

Hexdump.cs

a guest
May 7th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. /* Hexdump.cs
  2.  * A simple command-line Hex dumper
  3.  *
  4.  * Copyright (c) 2016 Rubyist
  5.  * This work is free. You can redistribute it and/or modify it under the
  6.  * terms of the Do What The Fuck You Want To Public License, Version 2,
  7.  * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
  8.  */
  9.  
  10. using System;
  11. using System.IO;
  12.  
  13. public class Hexdump
  14. {
  15.     public static void Main(String[] args)
  16.     {
  17.         /* Print usage information */
  18.         if (args.Length != 1)
  19.         {
  20.             Usage();
  21.         }
  22.  
  23.         if (args[0] == "--help" || args[0] == "-h")
  24.         {
  25.             Usage();
  26.         }
  27.  
  28.         /* Open and dump file */
  29.         try
  30.         {
  31.             FileStream file = File.Open(args[0], FileMode.Open);
  32.             DumpFile(file);
  33.             file.Close();
  34.         }
  35.         catch (Exception)
  36.         {
  37.             Console.WriteLine("Could not open file");
  38.             Environment.Exit(-1);
  39.         }
  40.        
  41.  
  42.     }
  43.  
  44.     static void PrintAscii(byte[] buffer, int len)
  45.     {
  46.         /* Print excess spaces for last line of a hex dump */
  47.         if (len < 16)
  48.         {
  49.             int spaces = (((16 - len) * 5) / 2) + (len % 2);
  50.             for (int i = 0; i < spaces; i++)
  51.             {
  52.                 Console.Write(' ');
  53.             }
  54.         }
  55.  
  56.         /* Print ascii values of all non-whitespace 7-bit ascii chars */
  57.         for (int i = 0; i < len; i++)
  58.         {
  59.             if (buffer[i] < 32 || buffer[i] > 126)
  60.             {
  61.                 Console.Write('.');
  62.             }
  63.             else
  64.             {
  65.                 Console.Write(Convert.ToChar(buffer[i]));
  66.             }
  67.         }
  68.     }
  69.  
  70.     static void Usage()
  71.     {
  72.         Console.WriteLine("usage: Hexdump binfile");
  73.         Environment.Exit(0);
  74.     }
  75.  
  76.     static void DumpFile(FileStream file)
  77.     {
  78.         /* Write header */
  79.         Console.WriteLine(
  80.             "Offset:             " +
  81.             "Bytes:                                  " +
  82.             "ASCII:"
  83.         );
  84.  
  85.         /* Write the following information:
  86.          * Offset: 64 bit value represented in hex.
  87.          * Bytes: 8 groups of 2 bytes each, represented as 4 hex digits.
  88.          * ASCII: 7-bit ascii interpretation of data. Whitespace are '.'
  89.          */
  90.         byte[] buffer = new byte[16];
  91.         while (file.Position != file.Length)
  92.         {
  93.             Console.Write("0x{0}: ", file.Position.ToString("X16"));
  94.             int written = file.Read(buffer, 0, 16);
  95.             for (int i = 0; i < written; i++)
  96.             {
  97.                 Console.Write(buffer[i].ToString("X2"));
  98.                 if (i%2 == 1)
  99.                 {
  100.                     Console.Write(' ');
  101.                 }
  102.             }
  103.             PrintAscii(buffer, written);
  104.             Console.WriteLine("");
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement