Thursday 30 June 2011

C# - Byte Array Extensions - ToHexString

using System.Text;

namespace My.Helpers
{
    public static class ByteExtensions
    {
        public static string ToHexString(this byte[] array)
        {
            // Create a new StringBuilder to collect the bytes and create a string.
            var sBuilder = new StringBuilder();

            // Convert to a hex string.
            for (int i = 0; i < array.Length; i++)
                sBuilder.Append(array[i].ToString("x2"));

            // Return the hex string.
            return sBuilder.ToString();
        }
    }
}

No comments:

Post a Comment