C_sharp / ProgTips / DebugDump


xxd風16進ダンプ

コード

using System;

namespace DumpLib
{
	/// <summary>
	/// デバッグダンプを行う
	/// </summary>
	public class DebugDump
	{
		public static string GetDumpStringStyleXxd(byte[] buffer, int cols)
		{
			string dumpString = "";

			for(int i=0; i<buffer.Length; i++)
			{
				if( (i % cols) == 0 )
				{
					if(i != 0)
					{
						dumpString += "  ";
						for(int j=cols; j>0; j--)
						{
							if( (buffer[i-j] > 0x20) && (buffer[i-j] < 0x7f) )
                                dumpString += (char)buffer[i-j];
							else
								dumpString += ".";
						}
 						dumpString += "\n";
					}
					dumpString += Convert.ToString(i, 16).PadLeft(8, '0') + ":";
				}

				if( (i % 2) == 0 )
				{
					dumpString += " ";
				}
				dumpString += Convert.ToString((long)buffer[i], 16).PadLeft(2, '0');
			}

			for(int i=buffer.Length%cols; i<cols; i++)
			{
				if( (i % 2) == 0 )
				{
					dumpString += " ";
				}
				dumpString += "  ";
			}
			dumpString += "  ";
			
			for(int i=buffer.Length%cols-1; i>=0; i--)
			{
				if( (buffer[buffer.Length-1-i] > 0x20) && (buffer[buffer.Length-1-i] < 0x7f) )
					dumpString += (char)buffer[buffer.Length-1-i];
				else
					dumpString += ".";
			}

			return dumpString;
		}
	}
}

利用例

コード

string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.Diagnostics.Debug.WriteLine(
	DumpLib.DebugDump.GetDumpStringStyleXxd(System.Text.Encoding.ASCII.GetBytes(s), 16));

byte [] buffer = {(byte)'a', (byte)'b', (byte)'c', (byte)0};
System.Diagnostics.Debug.WriteLine(DumpLib.DebugDump.GetDumpStringStyleXxd(buffer, 16));

出力

00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70  abcdefghijklmnop
00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70  abcdefghijklmnop
00000010: 7172 7374 7576 7778 797a 4142 4344 4546  qrstuvwxyzABCDEF
00000020: 4748 494a 4b4c 4d4e 4f50 5152 5354 5556  GHIJKLMNOPQRSTUV
00000030: 5758 595a                                WXYZ
00000000: 6162 6300                                abc.

MenuBar


最新の20件

2015-05-15 2014-12-07 2008-11-17 2007-08-30 2007-04-21 2007-03-13 2007-03-12 2007-02-20 2007-02-14 2007-02-04 2006-11-26 2006-11-23 2006-11-20 2006-11-18 2006-11-13

  • counter: 401
  • today: 1
  • yesterday: 0
  • online: 1