2018-12-11 13:32:01 -05:00
using System ;
namespace Vanara.Extensions
{
/// <summary>Extension to dump a byte array.</summary>
public static class HexDempHelpers
{
/// <summary>Creates a multi-line dump of a byte array using hexadecimal values.</summary>
/// <param name="bytes">The byte array to dump. This value cannot be <see langword="null"/>.</param>
/// <param name="bytesPerRow">The number of bytes to display on each line.</param>
/// <param name="gapEvery">The number of bytes to display before inserting an extra space to create a visual gap.</param>
/// <param name="rowIdLen">
/// The number of hexadecimal digits to display on the left side of each line to indicate position. If this value is 0, then no
2019-04-08 13:24:19 -04:00
/// position indicator will be shown. If this value is -1, then the size will be computed in increments of 4 based on the size of <paramref name="bytes"/>.
2018-12-11 13:32:01 -05:00
/// </param>
2019-07-03 16:42:51 -04:00
/// <param name="location">
/// A pointer location to show to the left of the row identifier. If set to <c>IntPtr.Zero</c>, no location value is shown.
/// </param>
2018-12-11 13:32:01 -05:00
/// <returns>A multi-line string that contains a hexadecimal dump of <paramref name="bytes"/>.</returns>
2019-07-03 16:42:51 -04:00
/// <exception cref="System.ArgumentNullException">bytes</exception>
2018-12-11 13:32:01 -05:00
/// <exception cref="ArgumentNullException">bytes</exception>
2019-07-03 16:42:51 -04:00
public static string ToHexDumpString ( this byte [ ] bytes , int bytesPerRow = 16 , int gapEvery = 4 , int rowIdLen = - 1 , IntPtr location = default )
2018-12-11 13:32:01 -05:00
{
if ( bytes = = null ) throw new ArgumentNullException ( nameof ( bytes ) ) ;
var sb = new System . Text . StringBuilder ( ) ;
var hdrlen = rowIdLen = = - 1 ? ( ( ( bytes . Length . ToString ( "X" ) . Length - 1 ) / 4 ) + 1 ) * 4 : rowIdLen ;
2019-07-03 16:42:51 -04:00
var hdrfmt = hdrlen = = 0 ? "" : $"{{0:X{hdrlen}}}: " ;
if ( location ! = IntPtr . Zero ) hdrfmt = $"0x{{1:X{IntPtr.Size * 2}}} " + hdrfmt ;
2018-12-11 13:32:01 -05:00
for ( var l = 0 ; l < bytes . Length ; l + = bytesPerRow )
{
2019-07-03 16:42:51 -04:00
sb . AppendFormat ( hdrfmt , l , location . Offset ( l ) . ToInt64 ( ) ) ;
2018-12-11 13:32:01 -05:00
for ( var i = l ; i < bytes . Length & & i < l + bytesPerRow ; i + + )
{
sb . Append ( $"{bytes[i]:X2} " ) ;
if ( ( i + 1 ) % gapEvery = = 0 ) sb . Append ( " " ) ;
}
sb . AppendLine ( ) ;
}
return sb . ToString ( ) ;
}
2019-04-08 13:24:19 -04:00
2019-07-03 16:42:51 -04:00
/// <summary>
/// Creates a multi-line dump of a byte array using hexadecimal values.
/// </summary>
2019-04-08 13:24:19 -04:00
/// <param name="ptr">A pointer to the memory to dump.</param>
2019-07-03 16:42:51 -04:00
/// <param name="byteCount">The number of bytes to display starting at the location pointed to by <paramref name="ptr" />.</param>
2019-04-08 13:24:19 -04:00
/// <param name="bytesPerRow">The number of bytes to display on each line.</param>
/// <param name="gapEvery">The number of bytes to display before inserting an extra space to create a visual gap.</param>
2019-07-03 16:42:51 -04:00
/// <param name="rowIdLen">The number of hexadecimal digits to display on the left side of each line to indicate position. If this value is 0, then no
/// position indicator will be shown. If this value is -1, then the size will be computed in increments of 4 based on the size of <paramref name="byteCount" />.</param>
/// <param name="showLocation">If set to <c>true</c>, the pointer location value is shown to the left of the row identifier.</param>
/// <returns>
/// A multi-line string that contains a hexadecimal dump of <paramref name="ptr" />.
/// </returns>
2019-04-08 13:24:19 -04:00
/// <exception cref="ArgumentNullException">bytes</exception>
2019-07-03 16:42:51 -04:00
public static string ToHexDumpString ( this IntPtr ptr , int byteCount , int bytesPerRow = 16 , int gapEvery = 4 , int rowIdLen = - 1 , bool showLocation = true ) = >
ptr . ToArray < byte > ( byteCount ) . ToHexDumpString ( bytesPerRow , gapEvery , rowIdLen , showLocation ? ptr : IntPtr . Zero ) ;
2018-12-11 13:32:01 -05:00
}
}