Source/NFX/MiscUtils.cs
/// <summary>
/// Convert a buffer to a printable string
/// </summary>
/// <param name="buf">Buffer to convert</param>
/// <param name="fmt">Dumping format</param>
/// <param name="offset">Starting index</param>
/// <param name="count">Number of bytes to process (-1 means all bytes in the buffer)</param>
/// <param name="eol">If true, terminate with end-of-line</param>
/// <param name="encoding">Encoding to use for writing data in Binary format</param>
/// <param name="maxLen">Max length of the returned string. Pass 0 for unlimited length</param>
/// <returns></returns>
Convert a buffer to a printable string
Convert a buffer to a printable string
public static string ToDumpString(this byte[] buf, DumpFormat fmt, int offset = 0,
        int count = -1, bool eol = false, Encoding encoding = null, int maxLen = 0)
    {
        if (count == -1) count = buf.Length - offset;
        int n;
        switch (fmt)
        {
            case DumpFormat.Decimal:    n = count * 4 + 4; break;
            case DumpFormat.Hex:        n = count * 3; break;
            case DumpFormat.Printable:  n = count * 4; break;
            default:                    throw new NFXException(StringConsts.OPERATION_NOT_SUPPORTED_ERROR + fmt.ToString());
        }
        var sb = new StringBuilder(n);
        int k = offset+count;
        int m = maxLen > 0 ? Math.Max(2, maxLen) : Int16.MaxValue;
        bool shrink = maxLen > 0;
        switch (fmt)
        {
            case DumpFormat.Decimal:
                m -= 2;
                sb.Append("<<");
                for (int i = offset; i < k && sb.Length+1 < m; i++)
                {
                    sb.Append(buf[i]);
                    sb.Append(',');
                }
                if (sb.Length > 2)
                    sb.Remove(sb.Length-1, 1);
                sb.Append(!shrink || sb.Length+1 < m ? ">>" : "...>>");
                break;
            case DumpFormat.Hex:
                m -= 3;
                for (int i = offset, j = 0; i < k && sb.Length < m; i++, j++)
                    sb.AppendFormat("{0:X2}{1}", buf[i], (j & 3) == 3 ? " " : "");
                if (sb.Length > 0 && sb[sb.Length-1] == ' ')
                    sb.Remove(sb.Length-1, 1);
                if (shrink) sb.Append("...");
                break;
            case DumpFormat.Printable:
                m -= 3;
                for (int i = offset; i < k && sb.Length < m; i++)
                {
                    byte c = buf[i];
                    if (c >= 32 && c < 127)
                        sb.Append((char)c);
                    else
                        switch (c)
                        {
                            case 10: sb.Append("\n"); break;
                            case 13: sb.Append("\r"); break;
                            case  8: sb.Append("\t"); break;
                            default: sb.AppendFormat("\\{0,3:D3}", c); break;
                        }
                }
                if (shrink) sb.Append("...");
                break;
        }
        if (eol)
            sb.Append('\n');
        return sb.ToString();
    }
public static string ToDumpString ( this byte [ ] buf , DumpFormat fmt , int offset = 0 , int count = - 1 , bool eol = false , Encoding encoding = null , int maxLen = 0 ) { if ( count == - 1 ) count = buf . Length - offset ; int n ; switch ( fmt ) { case DumpFormat . Decimal : n = count * 4 + 4 ; break ; case DumpFormat . Hex : n = count * 3 ; break ; case DumpFormat . Printable : n = count * 4 ; break ; default : throw new NFXException ( StringConsts . OPERATION_NOT_SUPPORTED_ERROR + fmt . ToString ( ) ) ; } var sb = new StringBuilder ( n ) ; int k = offset + count ; int m = maxLen > 0 ? Math . Max ( 2 , maxLen ) : Int16 . MaxValue ; bool shrink = maxLen > 0 ; switch ( fmt ) { case DumpFormat . Decimal : m -= 2 ; sb . Append ( " " ) ; for ( int i = offset ; i < k && sb . Length + 1 < m ; i ++ ) { sb . Append ( buf [ i ] ) ; sb . Append ( ' ' ) ; } if ( sb . Length > 2 ) sb . Remove ( sb . Length - 1 , 1 ) ; sb . Append ( ! shrink || sb . Length + 1 < m ? " " : " " ) ; break ; case DumpFormat . Hex : m -= 3 ; for ( int i = offset , j = 0 ; i < k && sb . Length < m ; i ++ , j ++ ) sb . AppendFormat ( " " , buf [ i ] , ( j & 3 ) == 3 ? " " : " " ) ; if ( sb . Length > 0 && sb [ sb . Length - 1 ] == ' ' ) sb . Remove ( sb . Length - 1 , 1 ) ; if ( shrink ) sb . Append ( " " ) ; break ; case DumpFormat . Printable : m -= 3 ; for ( int i = offset ; i < k && sb . Length < m ; i ++ ) { byte c = buf [ i ] ; if ( c >= 32 && c < 127 ) sb . Append ( ( char ) c ) ; else switch ( c ) { case 10 : sb . Append ( " \n " ) ; break ; case 13 : sb . Append ( " \r " ) ; break ; case 8 : sb . Append ( " \t " ) ; break ; default : sb . AppendFormat ( " \\ " , c ) ; break ; } } if ( shrink ) sb . Append ( " " ) ; break ; } if ( eol ) sb . Append ( ' \n ' ) ; return sb . ToString ( ) ; }
Convert a buffer to a printable string
Convert a buffer to a printable string
/*comma*/
None
byte[]
this
fmt
DumpFormat
None
offset
int
None
count
int
Number of bytes to process (-1 means all bytes in the buffer)
Number of bytes to process ( - 1 means all bytes in the buffer )
eol
bool
If true, terminate with end-of-line
If true terminate with end - of - line
encoding
Encoding
Encoding to use for writing data in Binary format
Encoding to use for writing data in Binary format
maxLen
int
Max length of the returned string. Pass 0 for unlimited length
Max length of the returned string . Pass 0 for unlimited length
buf
Buffer to convert
Buffer to convert