OpenSim/Framework/Util.cs
/// <summary>
/// Convert a variable length field (byte array) to a string, with a
/// field name prepended to each line of the output
/// </summary>
/// <remarks>If the byte array has unprintable characters in it, a
/// hex dump will be put in the string instead</remarks>
/// <param name="bytes">The byte array to convert to a string</param>
/// <param name="fieldName">A field name to prepend to each line of output</param>
/// <returns>An ASCII string or a string containing a hex dump, minus
/// the null terminator</returns>
Convert a variable length field (byte array) to a string, with a
field name prepended to each line of the output
Convert a variable length field ( byte array ) to a string with a field name prepended to each line of the output
public static string FieldToString(byte[] bytes, string fieldName)
        {
            if (bytes.Length == 0) return String.Empty;
            StringBuilder output = new StringBuilder();
            bool printable = true;
            for (int i = 0; i < bytes.Length; ++i)
            {
                if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
                    && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
                {
                    printable = false;
                    break;
                }
            }
            if (printable)
            {
                if (fieldName.Length > 0)
                {
                    output.Append(fieldName);
                    output.Append(": ");
                }
                output.Append(CleanString(Util.UTF8.GetString(bytes, 0, bytes.Length - 1)));
            }
            else
            {
                for (int i = 0; i < bytes.Length; i += 16)
                {
                    if (i != 0)
                        output.Append(Environment.NewLine);
                    if (fieldName.Length > 0)
                    {
                        output.Append(fieldName);
                        output.Append(": ");
                    }
                    for (int j = 0; j < 16; j++)
                    {
                        if ((i + j) < bytes.Length)
                            output.Append(String.Format("{0:X2} ", bytes[i + j]));
                        else
                            output.Append("   ");
                    }
                    for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
                    {
                        if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
                            output.Append((char) bytes[i + j]);
                        else
                            output.Append(".");
                    }
                }
            }
            return output.ToString();
        }
public static string FieldToString ( byte [ ] bytes , string fieldName ) { if ( bytes . Length == 0 ) return String . Empty ; StringBuilder output = new StringBuilder ( ) ; bool printable = true ; for ( int i = 0 ; i < bytes . Length ; ++ i ) { if ( ( bytes [ i ] < 0x20 || bytes [ i ] > 0x7E ) && bytes [ i ] != 0x09 && bytes [ i ] != 0x0D && bytes [ i ] != 0x0A && bytes [ i ] != 0x00 ) { printable = false ; break ; } } if ( printable ) { if ( fieldName . Length > 0 ) { output . Append ( fieldName ) ; output . Append ( " " ) ; } output . Append ( CleanString ( Util . UTF8 . GetString ( bytes , 0 , bytes . Length - 1 ) ) ) ; } else { for ( int i = 0 ; i < bytes . Length ; i += 16 ) { if ( i != 0 ) output . Append ( Environment . NewLine ) ; if ( fieldName . Length > 0 ) { output . Append ( fieldName ) ; output . Append ( " " ) ; } for ( int j = 0 ; j < 16 ; j ++ ) { if ( ( i + j ) < bytes . Length ) output . Append ( String . Format ( " " , bytes [ i + j ] ) ) ; else output . Append ( " " ) ; } for ( int j = 0 ; j < 16 && ( i + j ) < bytes . Length ; j ++ ) { if ( bytes [ i + j ] >= 0x20 && bytes [ i + j ] < 0x7E ) output . Append ( ( char ) bytes [ i + j ] ) ; else output . Append ( " " ) ; } } } return output . ToString ( ) ; }
Convert a variable length field (byte array) to a string, with a
field name prepended to each line of the output
Convert a variable length field ( byte array ) to a string with a field name prepended to each line of the output
// Check for a common case // Check if there are any unprintable characters in the array
An ASCII string or a string containing a hex dump, minus
the null terminator
An ASCII string or a string containing a hex dump minus the null terminator
bytes
byte[]
The byte array to convert to a string
The byte array to convert to a string
fieldName
string
A field name to prepend to each line of output
A field name to prepend to each line of output
remarks
If the byte array has unprintable characters in it, a
hex dump will be put in the string instead
If the byte array has unprintable characters in it a hex dump will be put in the string instead