V2/Cet/IO/Cet.IO/ByteArray/ByteArrayHelpers.Format.NET.cs
/// <summary>
/// Convert a byte-array to string containing its HEX-digit representation
/// formatted as it were a "memory-dump"
/// </summary>
/// <param name="source">The byte-array to be converted.</param>
/// <param name="address">The equivalent "address" from where the dump should start</param>
/// <returns>The resulting string containing the HEX representation</returns>
/// <remarks>
/// <para>This way to format the byte-array is particularly useful for memory-dumps.</para>
/// <para>The address is treated always as a 32-bit value, thus always 8-digits zeroed-padded.
/// When the address value overflows, then rolls back to zero.</para>
/// </remarks>
/// <example>Here is an example:
///     <code>
///     //create a sequence of 40 bytes (shortened for ease of representation)
///     byte[] buffer = new byte[40] { 0, 1, 2, ... , 38, 39 };
/// 
///     //convert the sequence, then display it
///     string s = ByteArray.DumpHex(0x134);
///     Console.WriteLine(s);
///
///     </code>
///     Yields:
///     <code>
///     00000130:             00 01 02 03-04 05 06 07 08 09 0A 0B
///     00000140: 0C 0D 0E 0F 10 11 12 13-14 15 16 17 18 19 1A 1B
///     00000150: 1C 1D 1E 1F 20 21 22 23-24 25 26 27
///     </code>
/// </example>
Convert a byte-array to string containing its HEX-digit representation
formatted as it were a "memory-dump"
Convert a byte - array to string containing its HEX - digit representation formatted as it were a " memory - dump "
public static string DumpHex(
            this IEnumerable<byte> source,
            int address)
        {
            char[] crlf = Environment.NewLine.ToCharArray();
            int crlfLen = crlf.Length;
            var buffer = source.ToArray();
            int count = buffer.Length;
            int rows = 2 + count / 16;
            int total = rows * (8 + 1 + 3 * 16 + crlfLen);
            char[] ca = new char[total];
            int len = 0;
            IEnumerator<byte> buffEnum = source.GetEnumerator();
            bool eof = false;
            bool started = false;
            Action<byte> pushByte = (value) =>
            {
                ca[len] = _tbltohex[unchecked(value >> 4)];
                ca[len + 1] = _tbltohex[unchecked(value & 0x0F)];
                len += 2;
            };
            Action<uint> pushAddress = (addr) =>
            {
                pushByte(unchecked((byte)(addr >> 24)));
                pushByte(unchecked((byte)(addr >> 16)));
                pushByte(unchecked((byte)(addr >> 8)));
                pushByte(unchecked((byte)(addr)));
                ca[len] = ':';
                len++;
            };
            var ptr = unchecked((uint)address);
            do
            {
                uint offset = ptr & (uint)0xFFFFFFF0;
                pushAddress(offset);
                int numBlanks;
                if (started)
                {
                    numBlanks = 0;
                }
                else
                {
                    numBlanks = unchecked((int)(address & 0x0F));
                    for (int i = 0; i < numBlanks; i++)
                    {
                        ca[len] = (i == 8) ? '-' : ' ';
                        ca[len + 1] = ' ';
                        ca[len + 2] = ' ';
                        len += 3;
                    }
                    started = true;
                }
                for (int i = numBlanks; i < 0x10; i++)
                {
                    if (buffEnum.MoveNext() == false)
                    {
                        eof = true;
                        break;
                    }
                    ca[len] = (i == 8) ? '-' : ' ';
                    len++;
                    pushByte(buffEnum.Current);
                }
                for (int i = 0; i < crlfLen; i++)
                    ca[len + i] = crlf[i];
                len += crlfLen;
                unchecked
                {
                    ptr = offset + 0x10;
                }
            } while (eof == false);
            Array.Resize(ref ca, len);
            return new string(ca);
        }
public static string DumpHex ( this IEnumerable < byte > source , int address ) { char [ ] crlf = Environment . NewLine . ToCharArray ( ) ; int crlfLen = crlf . Length ; var buffer = source . ToArray ( ) ; int count = buffer . Length ; int rows = 2 + count / 16 ; int total = rows * ( 8 + 1 + 3 * 16 + crlfLen ) ; char [ ] ca = new char [ total ] ; int len = 0 ; IEnumerator < byte > buffEnum = source . GetEnumerator ( ) ; bool eof = false ; bool started = false ; Action < byte > pushByte = ( value ) => { ca [ len ] = _tbltohex [ unchecked ( value >> 4 ) ] ; ca [ len + 1 ] = _tbltohex [ unchecked ( value & 0x0F ) ] ; len += 2 ; } ; Action < uint > pushAddress = ( addr ) => { pushByte ( unchecked ( ( byte ) ( addr >> 24 ) ) ) ; pushByte ( unchecked ( ( byte ) ( addr >> 16 ) ) ) ; pushByte ( unchecked ( ( byte ) ( addr >> 8 ) ) ) ; pushByte ( unchecked ( ( byte ) ( addr ) ) ) ; ca [ len ] = ' ' ; len ++ ; } ; var ptr = unchecked ( ( uint ) address ) ; do { uint offset = ptr & ( uint ) 0xFFFFFFF0 ; pushAddress ( offset ) ; int numBlanks ; if ( started ) { numBlanks = 0 ; } else { numBlanks = unchecked ( ( int ) ( address & 0x0F ) ) ; for ( int i = 0 ; i < numBlanks ; i ++ ) { ca [ len ] = ( i == 8 ) ? ' ' : ' ' ; ca [ len + 1 ] = ' ' ; ca [ len + 2 ] = ' ' ; len += 3 ; } started = true ; } for ( int i = numBlanks ; i < 0x10 ; i ++ ) { if ( buffEnum . MoveNext ( ) == false ) { eof = true ; break ; } ca [ len ] = ( i == 8 ) ? ' ' : ' ' ; len ++ ; pushByte ( buffEnum . Current ) ; } for ( int i = 0 ; i < crlfLen ; i ++ ) ca [ len + i ] = crlf [ i ] ; len += crlfLen ; unchecked { ptr = offset + 0x10 ; } } while ( eof == false ) ; Array . Resize ( ref ca , len ) ; return new string ( ca ) ; }
Convert a byte-array to string containing its HEX-digit representation
formatted as it were a "memory-dump"
Convert a byte - array to string containing its HEX - digit representation formatted as it were a " memory - dump "
//convert the new-line seuqnce in a character-array //count the involved characters //estimate (in excess) the allocation size for the resulting string //favor operating on a char-array, for a better performance //define the enumerator, and some flags //define an handler for converting a value in a HEX-digits pair //define an handler for converting the address in a HEX-digits octet //cycle creating rows, until all the data is over //calculate the "offset" address //the row begins with the address //calculate how many "blanks" have to be inserted before the data bytes //after the first row, there's no more need of blanks //the blanks are used only in the first row //add the data digits, up to the end of row //valid byte //switch to a new line //update the offset //adjust the char-array size //return the resulting string
The resulting string containing the HEX representation
The resulting string containing the HEX representation
IEnumerable<byte>
this
address
int
The equivalent "address" from where the dump should start
The equivalent " address " from where the dump should start
source
The byte-array to be converted.
The byte - array to be converted .
remarks
This way to format the byte-array is particularly useful for memory-dumps.The address is treated always as a 32-bit value, thus always 8-digits zeroed-padded.
When the address value overflows, then rolls back to zero.
This way to format the byte - array is particularly useful for memory - dumps . The address is treated always as a 32 - bit value thus always 8 - digits zeroed - padded . When the address value overflows then rolls back to zero .
example
Here is an example.

create a sequence of 40 bytes (shortened for ease of representation)
byte[] buffer = new byte[40] { 0, 1, 2, ...

convert the sequence, then display it
string s = ByteArray.DumpHex(0x134);
Console.WriteLine(s).


Here is an example . create a sequence of 40 bytes ( shortened for ease of representation ) byte [] buffer = new byte [ 40 ] { 0 1 2 ... convert the sequence then display it string s = ByteArray . DumpHex ( 0x134 ) ; Console . WriteLine ( s ) .