Converts to a string containing the buffer described in hex - CSharp System

CSharp examples for System:Hex

Description

Converts to a string containing the buffer described in hex

Demo Code


using System.Text;
using System.Collections.Generic;
using System;//from  w w w  . j  a  v  a 2  s . com

public class Main{
        /// <summary>
        /// Converts to a string containing the buffer described in hex
        /// </summary>
        public static string DescribeAsHex(byte[] buffer, string separator, int bytesPerLine)
        {
            StringBuilder sb = new StringBuilder();
            int n = 0;
            foreach (byte b in buffer)
            {
                sb.AppendFormat("{0:X2}{1}", b, separator);
                if (++n % bytesPerLine == 0)
                    sb.Append("\r\n");
            }
            sb.Append("\r\n");
            return sb.ToString();
        }
}

Related Tutorials